Skip to content

How can I avoid using ”conda run“ for executing my code? #839

@syhgzz

Description

@syhgzz

Video record: https://youtu.be/9EYftXGjwQ0

When I use the Python Environment Extension in VS Code and select a Conda interpreter, clicking the "Run" button executes the command conda run --name myenv python /path/to/mycode.py in the terminal. I've noticed that this method, which uses conda run, causes significant slowdowns, even stuck in my PyTorch code, particularly when using the DataLoader. This issue seems specific to the ”conda run“ command, as running the same code directly with python mycode.py works perfectly fine and much faster. The same slowdown occurs if I manually type conda run in my macOS terminal.

How can I avoid using ”conda run“ for executing my code?

  import pylib as pl
  
  
  import torch
  import torchvision
  import torch.nn as nn
  
  
  
  def data_loader(batch_size,device):
      train_batch_size = batch_size
      test_batch_size = batch_size
      tran = torchvision.transforms.ToTensor()
      resize = None
      num_workers = 4
      if resize:
          tran = torchvision.transforms.Compose([torchvision.transforms.Resize(resize),tran])
      train_dataset = torchvision.datasets.FashionMNIST(root="./data",train=True,transform=tran,download=True)
      test_dataset = torchvision.datasets.FashionMNIST(root="./data",train=False,transform=tran,download=True)
      # train_dataset = torchvision.datasets.MNIST(root="./data",train=True,transform=tran,download=True)
      # test_dataset = torchvision.datasets.MNIST(root="./data",train=False,transform=tran,download=True)
      train_iter = torch.utils.data.DataLoader(train_dataset,train_batch_size,shuffle=True,num_workers=num_workers,pin_memory=True)
      test_iter = torch.utils.data.DataLoader(test_dataset,test_batch_size,shuffle=False,num_workers=num_workers,pin_memory=True)
      train_iter = pl.data_prefetcher(train_iter,device)
      test_iter = pl.data_prefetcher(test_iter,device)
  
      return train_iter, test_iter
  
  
  def epoch_train(train_iter,
                  net,
                  loss,
                  optimizer,
                  device='cpu',
                  epoch=0
                  ):
      net.train()
      metric_epoch = pl.Accumulator(3)
      for x, y in train_iter:
          # x = x.to(device)
          # y = y.to(device)
  
          y_hat = net(x)
          l = loss(y_hat,y)
          optimizer.zero_grad()
          l.mean().backward()
          optimizer.step()
          
          # y_hat = y_hat.to("cpu")
          acc_sum = pl.accuracy_sum(y_hat,y)
          metric_epoch.add(acc_sum,l.sum(),y.numel())   
  
      return metric_epoch
  
  
  @pl.timer('Training')
  def train(num_epoch,
            epoch_train,
            train_iter,
            test_iter,
            net,
            loss,
            optimizer,
            device
            ):
  
      et = pl.timer('',True)(epoch_train)
      s1 = torch.cuda.Stream()
      s2 = torch.cuda.Stream()
      for epoch in range(num_epoch):
  
    
          with torch.cuda.stream(s1):
              m,t = et(train_iter,net,loss,optimizer,device)
  
          with torch.cuda.stream(s2):
              pl.epoch_eval(net,test_iter,m,t,epoch,device)
      torch.cuda.synchronize(s1)
      torch.cuda.synchronize(s2)
  
  
  def init_weight(m):
      if type(m) == nn.Linear or type(m) == nn.Conv2d:
          nn.init.xavier_uniform_(m.weight)
  
  def init_network2(device):
      layer = [nn.Flatten(),
              nn.Linear(28*28,1200),nn.ReLU(),
              nn.Linear(1200,1200),nn.ReLU(),
              nn.Linear(1200,10)]
      net = nn.Sequential(*layer)
  
  
      net.apply(pl.initWeight)
      net.to(device,non_blocking=True)
      return net
  def init_LeNet(device):
      p  = 0.1
      layer = [ nn.Conv2d(1,6,kernel_size=5,padding=2), nn.Sigmoid(),
               nn.AvgPool2d(kernel_size=2,stride=2),
              nn.Conv2d(6,16,kernel_size=5), nn.Sigmoid(),
               nn.AvgPool2d(kernel_size=2,stride=2),
               nn.Flatten(),
               nn.Linear(16*5*5,120),nn.Sigmoid(),
               nn.Linear(120,84),nn.Sigmoid(),
               nn.Linear(84,10)
      ]
      net = nn.Sequential(*layer)
  
  
      net.apply(init_weight)
      net.to(device,non_blocking=True)
      return net
  
  def init_loss():
      loss = nn.CrossEntropyLoss(reduction='none')
      return loss
  
  
  def init_optimizer(net,lr):
      o = torch.optim.SGD(net.parameters(),lr=lr)
      return o
  
  def init_optimizer2(net,lr):
      wd  = 0.1
      # [print(type(net[i])) for i in range(5)]
      o = torch.optim.SGD([{"params":net[1].weight,'weight_decay':wd},{"params":net[1].bias},
                           {"params":net[3].weight,'weight_decay':wd},{"params":net[3].bias},
                           {"params":net[5].weight,'weight_decay':wd},{"params":net[5].bias}],
                           lr=lr)
      return o
  
  def predict(test_iter,net,device):
      xx,yy = next(iter(test_iter))
      # xx = xx.to(device)
      # yy = yy.to(device)
      get_label_func = pl.get_fashion_mnist_labels
      y_str = get_label_func(yy)
  
      y_hat_str = get_label_func(net(xx).argmax(axis=1)) 
      titles = [t + '\n' + p for t,p in zip(y_str,y_hat_str) ]
      # show_images(xx,3,3,scale=1)
      xx = xx.to('cpu')
      pl.show_images(xx,5,5,titles=titles )
  
  
  
  if __name__=="__main__":
      
      m = pl.Model(batch_size=256,lr=0.9,num_epoch=100) 
      
      m.set_GPU()
      
      m.data_loader = data_loader
      m.init_network = init_LeNet
      m.init_loss = init_loss
      m.init_optimizer = init_optimizer
      m.train = train
      m.epoch_train = epoch_train
              
  
      m.train_go()
  
      m.predict(m.test_iter,predict)




Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bugverifiedVerification succeeded

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions