コード例 #1
0
        public void CanCreateJob()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string groupName   = SdkContext.RandomResourceName("rg", 10);
                string clusterName = SdkContext.RandomResourceName("cluster", 15);
                string userName    = "******";

                var manager = TestHelper.CreateBatchAIManager();

                IBatchAICluster cluster = manager.BatchAIClusters.Define(clusterName)
                                          .WithRegion(REGION)
                                          .WithNewResourceGroup(groupName)
                                          .WithVMSize(VirtualMachineSizeTypes.StandardD1V2.Value)
                                          .WithUserName(userName)
                                          .WithPassword("MyPassword")
                                          .WithAutoScale(1, 1)
                                          .Create();
                Assert.Equal(AllocationState.Steady, cluster.AllocationState);
                Assert.Equal(userName, cluster.AdminUserName);
                IBatchAIJob job = manager.BatchAIJobs.Define("myJob")
                                  .WithExistingClusterId(cluster.Id)
                                  .WithNodeCount(1)
                                  .WithStdOutErrPathPrefix("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare")
                                  .DefineCognitiveToolkit()
                                  .WithPythonScriptFile("$AZ_BATCHAI_INPUT_SAMPLE/ConvNet_MNIST.py")
                                  .WithCommandLineArgs("$AZ_BATCHAI_INPUT_SAMPLE $AZ_BATCHAI_OUTPUT_MODEL")
                                  .Attach()
                                  .WithInputDirectory("SAMPLE", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/mnistcntksample")
                                  .WithOutputDirectory("MODEL", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/model")
                                  .DefineOutputDirectory("OUTPUT")
                                  .WithPathPrefix("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/output")
                                  .WithCreateNew(true)
                                  .WithPathSuffix("suffix")
                                  .Attach()
                                  .WithContainerImage("microsoft/cntk:2.1-gpu-python3.5-cuda8.0-cudnn6.0")
                                  .Create();
                Assert.Equal(groupName, job.ResourceGroupName);
                Assert.Equal(2, job.OutputDirectories.Count);
                OutputDirectory outputDirectory = null;
                foreach (OutputDirectory directory in job.OutputDirectories)
                {
                    if ("OUTPUT".Equals(directory.Id.ToUpper()))
                    {
                        outputDirectory = directory;
                        break;
                    }
                }
                Assert.NotNull(outputDirectory);
                Assert.Equal("suffix", outputDirectory.PathSuffix.ToLower());

                job.Refresh();
                Assert.Equal(groupName, job.ResourceGroupName);

                manager.ResourceManager.ResourceGroups.BeginDeleteByName(groupName);
            }
        }
コード例 #2
0
        public void CanCreateJob()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string groupName          = SdkContext.RandomResourceName("rg", 10);
                string clusterName        = SdkContext.RandomResourceName("cluster", 15);
                string saName             = SdkContext.RandomResourceName("sa", 15);
                string shareName          = "myfileshare";
                string shareMountPath     = "azurefileshare";
                string blobFileSystemPath = "myblobsystem";
                string containerName      = "mycontainer";
                string userName           = "******";
                string storageAccountKey  = "dummy_key";
                string fileShareUri       = "dummy_uri";

                var manager = TestHelper.CreateBatchAIManager();

                IBatchAICluster cluster = manager.BatchAIClusters.Define(clusterName)
                                          .WithRegion(REGION)
                                          .WithNewResourceGroup(groupName)
                                          .WithVMSize(VirtualMachineSizeTypes.StandardD1V2.Value)
                                          .WithUserName(userName)
                                          .WithPassword("MyPassword")
                                          .WithAutoScale(1, 1)
                                          .Create();
                Assert.Equal(AllocationState.Steady, cluster.AllocationState);
                Assert.Equal(userName, cluster.AdminUserName);
                IBatchAIJob job = cluster.Jobs.Define("myJob")
                                  .WithRegion(REGION)
                                  .WithNodeCount(1)
                                  .WithStdOutErrPathPrefix("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare")
                                  .DefineCognitiveToolkit()
                                  .WithPythonScriptFile("$AZ_BATCHAI_INPUT_SAMPLE/ConvNet_MNIST.py")
                                  .WithCommandLineArgs("$AZ_BATCHAI_INPUT_SAMPLE $AZ_BATCHAI_OUTPUT_MODEL")
                                  .Attach()
                                  .WithInputDirectory("SAMPLE", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/mnistcntksample")
                                  .WithOutputDirectory("MODEL", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/model")
                                  .WithContainerImage("microsoft/cntk:2.1-gpu-python3.5-cuda8.0-cudnn6.0")
                                  .Create();
                Assert.Equal(groupName, job.ResourceGroupName);

                job.Refresh();
                Assert.Equal(groupName, job.ResourceGroupName);

                manager.ResourceManager.ResourceGroups.DeleteByName(groupName);
            }
        }
コード例 #3
0
        /**
         * Azure Batch AI sample.
         *  - Create Storage account and Azure file share
         *  - Upload sample data to Azure file share
         *  - Create a workspace and experiment
         *  - Create Batch AI cluster that uses Azure file share to host the training data and scripts for the learning job
         *  - Create Microsoft Cognitive Toolkit job to run on the cluster
         *  - Wait for job to complete
         *  - Get output files
         */
        public static void RunSample(IAzure azure)
        {
            string saName         = SdkContext.RandomResourceName("sa", 10);
            string rgName         = SdkContext.RandomResourceName("rg", 10);
            string workspaceName  = SdkContext.RandomResourceName("ws", 10);
            string experimentName = SdkContext.RandomResourceName("exp", 10);
            string sampleDataPath = Environment.GetEnvironmentVariable("SAMPLE_DATA_PATH");
            Region region         = Region.USWest2;
            string shareName      = SdkContext.RandomResourceName("fs", 20);
            string clusterName    = SdkContext.RandomResourceName("cluster", 15);
            string userName       = Utilities.CreateUsername();
            string sharePath      = "mnistcntksample";

            try
            {
                //=============================================================
                // Create a new storage account and an Azure file share resource
                Utilities.Log("Creating a storage account...");
                IStorageAccount storageAccount = azure.StorageAccounts.Define(saName)
                                                 .WithRegion(region)
                                                 .WithNewResourceGroup(rgName)
                                                 .Create();
                Utilities.Log("Created storage account.");
                Utilities.PrintStorageAccount(storageAccount);

                StorageAccountKey storageAccountKey = storageAccount.GetKeys().First();

                Utilities.Log("Creating Azure File share...");
                var cloudFileShare = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={saName};AccountKey={storageAccountKey.Value};EndpointSuffix=core.windows.net")
                                     .CreateCloudFileClient()
                                     .GetShareReference(shareName);
                cloudFileShare.CreateAsync().GetAwaiter().GetResult();
                Utilities.Log("Created Azure File share.");

                //=============================================================
                // Upload sample data to Azure file share

                //Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = cloudFileShare.GetRootDirectoryReference();

                //Get a reference to the sampledir directory
                Utilities.Log("Creating directory and uploading data files...");
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(sharePath);
                sampleDir.CreateAsync().GetAwaiter().GetResult();

                rootDir.GetFileReference("Train-28x28_cntk_text.txt").UploadFromFileAsync(sampleDataPath + "/Train-28x28_cntk_text.txt").GetAwaiter().GetResult();
                rootDir.GetFileReference("Test-28x28_cntk_text.txt").UploadFromFileAsync(sampleDataPath + "/Test-28x28_cntk_text.txt").GetAwaiter().GetResult();
                rootDir.GetFileReference("ConvNet_MNIST.py").UploadFromFileAsync(sampleDataPath + "/ConvNet_MNIST.py").GetAwaiter().GetResult();
                Utilities.Log("Data files uploaded.");
                //=============================================================
                // Create a workspace and experiment
                IBatchAIWorkspace workspace = azure.BatchAIWorkspaces.Define(workspaceName)
                                              .WithRegion(region)
                                              .WithNewResourceGroup(rgName)
                                              .Create();
                IBatchAIExperiment experiment = workspace.CreateExperiment(experimentName);


                //=============================================================
                // Create Batch AI cluster that uses Azure file share to host the training data and scripts for the learning job
                Utilities.Log("Creating Batch AI cluster...");
                IBatchAICluster cluster = workspace.Clusters.Define(clusterName)
                                          .WithVMSize(VirtualMachineSizeTypes.StandardNC6.Value)
                                          .WithUserName(userName)
                                          .WithPassword("MyPassword")
                                          .WithAutoScale(0, 2)
                                          .DefineAzureFileShare()
                                          .WithStorageAccountName(saName)
                                          .WithAzureFileUrl(cloudFileShare.Uri.ToString())
                                          .WithRelativeMountPath("azurefileshare")
                                          .WithAccountKey(storageAccountKey.Value)
                                          .Attach()
                                          .Create();
                Utilities.Log("Created Batch AI cluster.");
                Utilities.Print(cluster);

                // =============================================================
                // Create Microsoft Cognitive Toolkit job to run on the cluster
                Utilities.Log("Creating Batch AI job...");
                IBatchAIJob job = experiment.Jobs.Define("myJob")
                                  .WithExistingCluster(cluster)
                                  .WithNodeCount(1)
                                  .WithStdOutErrPathPrefix("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare")
                                  .DefineCognitiveToolkit()
                                  .WithPythonScriptFile("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/ConvNet_MNIST.py")
                                  .WithCommandLineArgs("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare $AZ_BATCHAI_OUTPUT_MODEL")
                                  .Attach()
                                  .WithOutputDirectory("MODEL", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/model")
                                  .WithContainerImage("microsoft/cntk:2.1-gpu-python3.5-cuda8.0-cudnn6.0")
                                  .Create();
                Utilities.Log("Created Batch AI job.");
                Utilities.Print(job);

                // =============================================================
                // Wait for job results

                // Wait for job to start running
                Utilities.Log("Waiting for Batch AI job to start running...");
                while (ExecutionState.Queued.Equals(job.ExecutionState))
                {
                    SdkContext.DelayProvider.Delay(5000);
                    job.Refresh();
                }

                // Wait for job to complete and job output to become available
                Utilities.Log("Waiting for Batch AI job to complete...");
                while (!(ExecutionState.Succeeded.Equals(job.ExecutionState) || ExecutionState.Failed.Equals(job.ExecutionState)))
                {
                    SdkContext.DelayProvider.Delay(5000);
                    job.Refresh();
                }

                // =============================================================
                // Get output files

                // Print stdout and stderr
                foreach (var outputFile in job.ListFiles("stdouterr"))
                {
                    Utilities.Log(Utilities.CheckAddress(outputFile.DownloadUrl));
                }
                // List model output files
                foreach (var outputFile in job.ListFiles("MODEL"))
                {
                    Utilities.Log(outputFile.DownloadUrl);
                }
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (Exception)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
            }
        }