public void CanCreateFileServer()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string groupName  = SdkContext.RandomResourceName("rg", 10);
                string wsName     = SdkContext.RandomResourceName("ws", 10);
                string vnetName   = SdkContext.RandomResourceName("vnet", 10);
                string fsName     = SdkContext.RandomResourceName("fs", 15);
                string userName   = "******";
                string subnetName = "MySubnet";

                var manager        = TestHelper.CreateBatchAIManager();
                var networkManager = TestHelper.CreateNetworkManager();
                try
                {
                    IBatchAIWorkspace workspace = manager.BatchAIWorkspaces.Define(wsName)
                                                  .WithRegion(REGION)
                                                  .WithNewResourceGroup(groupName)
                                                  .Create();
                    INetwork network = networkManager.Networks.Define(vnetName)
                                       .WithRegion(REGION)
                                       .WithExistingResourceGroup(groupName)
                                       .WithAddressSpace("192.168.0.0/16")
                                       .WithSubnet(subnetName, "192.168.200.0/24")
                                       .Create();

                    IBatchAIFileServer fileServer = workspace.FileServers.Define(fsName)
                                                    .WithDataDisks(10, 2, Microsoft.Azure.Management.BatchAI.Fluent.Models.StorageAccountType.StandardLRS, CachingType.Readwrite)
                                                    .WithVMSize(VirtualMachineSizeTypes.StandardD1V2.Value)
                                                    .WithUserName(userName)
                                                    .WithPassword("MyPassword!")
                                                    .WithSubnet(network.Id, subnetName)
                                                    .Create();
                    Assert.Equal(network.Id + "/subnets/" + subnetName, fileServer.Subnet.Id);
                    Assert.Equal(CachingType.Readwrite, fileServer.DataDisks.CachingType);
                }
                finally
                {
                    try
                    {
                        manager.ResourceManager.ResourceGroups.BeginDeleteByName(groupName);
                    }
                    catch { }
                }
            }
        }
Esempio n. 2
0
 internal BatchAIJobImpl(string name, BatchAIExperimentImpl parent, JobInner inner) : base(name, inner)
 {
     this.workspace  = parent.Workspace();
     this.experiment = parent;
 }
        /**
         * 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");
                }
            }
        }
        public void CreateUpdate()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string groupName          = SdkContext.RandomResourceName("rg", 10);
                string workspaceName      = SdkContext.RandomResourceName("ws", 10);
                string clusterName        = SdkContext.RandomResourceName("cluster", 15);
                string vnetName           = SdkContext.RandomResourceName("vnet", 10);
                string saName             = SdkContext.RandomResourceName("sa", 15);
                string shareMountPath     = "azurefileshare";
                string blobFileSystemPath = "myblobsystem";
                string containerName      = "mycontainer";
                string userName           = "******";
                string storageAccountKey  = "dummy_key";
                string fileShareUri       = "dummy_uri";
                string subnetName         = "MySubnet";

                var manager        = TestHelper.CreateBatchAIManager();
                var networkManager = TestHelper.CreateNetworkManager();

                try
                {
                    IBatchAIWorkspace workspace = manager.BatchAIWorkspaces.Define(workspaceName)
                                                  .WithRegion(REGION)
                                                  .WithNewResourceGroup(groupName)
                                                  .Create();

                    INetwork network = networkManager.Networks.Define(vnetName)
                                       .WithRegion(REGION)
                                       .WithExistingResourceGroup(groupName)
                                       .WithAddressSpace("192.168.0.0/16")
                                       .WithSubnet(subnetName, "192.168.200.0/24")
                                       .Create();

                    IBatchAICluster cluster = workspace.Clusters.Define(clusterName)
                                              .WithVMSize(VirtualMachineSizeTypes.StandardD1V2.Value)
                                              .WithUserName(userName)
                                              .WithPassword("MyPassword")
                                              .WithAutoScale(1, 1)
                                              .WithLowPriority()
                                              .DefineSetupTask()
                                              .WithCommandLine("echo Hello World!")
                                              .WithStdOutErrPath("./outputpath")
                                              .Attach()
                                              .DefineAzureFileShare()
                                              .WithStorageAccountName(saName)
                                              .WithAzureFileUrl(fileShareUri)
                                              .WithRelativeMountPath(shareMountPath)
                                              .WithAccountKey(storageAccountKey)
                                              .Attach()
                                              .DefineAzureBlobFileSystem()
                                              .WithStorageAccountName(saName)
                                              .WithContainerName(containerName)
                                              .WithRelativeMountPath(blobFileSystemPath)
                                              .WithAccountKey(storageAccountKey)
                                              .Attach()
                                              .WithVirtualMachineImage("microsoft-ads", "linux-data-science-vm-ubuntu", "linuxdsvmubuntu")
                                              .WithSubnet(network.Id, subnetName)
                                              .WithAppInsightsComponentId("appinsightsId")
                                              .WithInstrumentationKey("appInsightsKey")
                                              .Create();
                    Assert.Equal(AllocationState.Resizing, cluster.AllocationState);
                    Assert.Equal(userName, cluster.AdminUserName);
                    Assert.Equal(VmPriority.Lowpriority, cluster.VMPriority);
                    Assert.Equal(1, cluster.NodeSetup.MountVolumes.AzureFileShares.Count);
                    Assert.Equal(shareMountPath,
                                 cluster.NodeSetup.MountVolumes.AzureFileShares.ElementAt(0).RelativeMountPath);
                    Assert.Equal(1, cluster.NodeSetup.MountVolumes.AzureBlobFileSystems.Count);
                    Assert.Equal(blobFileSystemPath,
                                 cluster.NodeSetup.MountVolumes.AzureBlobFileSystems.ElementAt(0).RelativeMountPath);
                    Assert.Equal(network.Id + "/subnets/" + subnetName, cluster.Subnet.Id);
                    Assert.Equal("appinsightsId",
                                 cluster.NodeSetup.PerformanceCountersSettings.AppInsightsReference.Component.Id);
                    Assert.Equal("linux-data-science-vm-ubuntu",
                                 cluster.VirtualMachineConfiguration.ImageReference.Offer);

                    cluster.Update()
                    .WithAutoScale(1, 2, 2)
                    .Apply();
                    Assert.Equal(2, cluster.ScaleSettings.AutoScale.MaximumNodeCount);
                }
                finally
                {
                    try
                    {
                        manager.ResourceManager.ResourceGroups.BeginDeleteByName(groupName);
                    }
                    catch { }
                }
            }
        }
        public void CanCreateJob()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string groupName      = SdkContext.RandomResourceName("rg", 10);
                string workspaceName  = SdkContext.RandomResourceName("ws", 10);
                string experimentName = SdkContext.RandomResourceName("exp", 10);
                string clusterName    = SdkContext.RandomResourceName("cluster", 15);
                string userName       = "******";

                var manager = TestHelper.CreateBatchAIManager();

                try
                {
                    IBatchAIWorkspace workspace = manager.BatchAIWorkspaces.Define(workspaceName)
                                                  .WithRegion(REGION)
                                                  .WithNewResourceGroup(groupName)
                                                  .Create();
                    IBatchAIExperiment experiment = workspace.CreateExperiment(experimentName);

                    IBatchAICluster cluster = workspace.Clusters.Define(clusterName)
                                              .WithVMSize(VirtualMachineSizeTypes.StandardD1V2.Value)
                                              .WithUserName(userName)
                                              .WithPassword("MyPassword")
                                              .WithAutoScale(1, 1)
                                              .Create();
                    Assert.Equal(AllocationState.Resizing, cluster.AllocationState);
                    Assert.Equal(userName, cluster.AdminUserName);
                    IBatchAIJob job = experiment.Jobs.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")
                                      .WithPathSuffix("suffix")
                                      .Attach()
                                      .WithContainerImage("microsoft/cntk:2.1-gpu-python3.5-cuda8.0-cudnn6.0")
                                      .Create();
                    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();
                }
                finally
                {
                    try
                    {
                        manager.ResourceManager.ResourceGroups.BeginDeleteByName(groupName);
                    }
                    catch { }
                }
            }
        }
Esempio n. 6
0
 internal BatchAIJobsImpl(BatchAIExperimentImpl experiment)
 {
     this.workspace  = experiment.Workspace();
     this.experiment = experiment;
 }