vathysjacob opened a new issue #6376:
URL: https://github.com/apache/incubator-tvm/issues/6376


   
   I have been playing with swapping in and out models and targets in an iOS 
app and noticed that when a Metal target model is loaded the app memory usage 
increases monotonically, in my case by about 22MB at a time.
   
   This is being caused by a Metal buffer being retained, but never released:
   
   
https://github.com/apache/incubator-tvm/blob/master/src/runtime/metal/metal_device_api.mm#L162
   
   Making the following change appears to resolve the issue for me, but I'm not 
yet familiar enough with Metal or iOS/Objective-C best practices to be 
comfortable offering my own PR.
   
   ```
     @@ -159,7 +159,7 @@ int GetWarpSize(id<MTLDevice> dev) {
      */
      id<MTLBuffer> buf = [dev newBufferWithLength:nbytes options:storage_mode];
      CHECK(buf != nil);
   -  return (__bridge void*)([buf retain]);
   +  return (__bridge void*)buf;
    }
    ```
   
   It is not clear to me if the other retain calls are actually necessary, 
perhaps someone more knowledgeable in this area can weigh in?
   
   
   ```
   diff --git a/src/runtime/metal/metal_device_api.mm 
b/src/runtime/metal/metal_device_api.mm
   index fddeadf86..0e8b8aae0 100644
   --- a/src/runtime/metal/metal_device_api.mm
   +++ b/src/runtime/metal/metal_device_api.mm
   @@ -126,14 +126,14 @@ int GetWarpSize(id<MTLDevice> dev) {
    #if TARGET_OS_IPHONE
      // on iPhone
      id<MTLDevice> d = MTLCreateSystemDefaultDevice();
   -  devices.push_back([d retain]);
   -  queues.push_back([[d newCommandQueue] retain]);
   +  devices.push_back(d );
   +  queues.push_back([d newCommandQueue] );
    #else
      NSArray<id<MTLDevice> >* devs = MTLCopyAllDevices();
      for (size_t i = 0; i < devs.count; ++i) {
        id<MTLDevice> d = [devs objectAtIndex:i];
   -    devices.push_back([d retain]);
   -    queues.push_back([[d newCommandQueue] retain]);
   +    devices.push_back(d );
   +    queues.push_back([d newCommandQueue]);
        LOG(INFO) << "Intializing Metal device " << i << ", name=" << [d.name 
UTF8String];
        warp_size.push_back(GetWarpSize(d));
      }
   @@ -159,7 +159,7 @@ int GetWarpSize(id<MTLDevice> dev) {
      */
      id<MTLBuffer> buf = [dev newBufferWithLength:nbytes options:storage_mode];
      CHECK(buf != nil);
   -  return (__bridge void*)([buf retain]);
   +  return (__bridge void*)buf;
    }
   
    void MetalWorkspace::FreeDataSpace(TVMContext ctx, void* ptr) {
   @@ -264,8 +264,8 @@ int GetWarpSize(id<MTLDevice> dev) {
        if (temp_buffer_[ctx.device_id] != nil) {
          [temp_buffer_[ctx.device_id] release];
        }
   -    temp_buffer_[ctx.device_id] = [[dev newBufferWithLength:size
   -                                                    
options:MTLStorageModeShared] retain];
   +    temp_buffer_[ctx.device_id] = [dev newBufferWithLength:size
   +                                                    
options:MTLStorageModeShared];
      }
      return temp_buffer_[ctx.device_id];
    }
    ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to