From 4b40fbd7430c85c65a93d5a6adaf22888b91c9dc Mon Sep 17 00:00:00 2001 From: Boyuan Yao <70263930+Cypher30@users.noreply.github.com> Date: Sun, 4 Dec 2022 15:00:16 +0800 Subject: [PATCH] [autoparallel] fix forward memory calculation (#2062) --- .../meta_profiler/meta_registry/activation.py | 10 ++++---- .../meta_profiler/meta_registry/conv.py | 24 +++++++++---------- .../meta_profiler/meta_registry/linear.py | 13 +++++----- .../meta_profiler/meta_registry/norm.py | 3 ++- .../meta_profiler/meta_registry/pooling.py | 3 ++- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/colossalai/auto_parallel/meta_profiler/meta_registry/activation.py b/colossalai/auto_parallel/meta_profiler/meta_registry/activation.py index a5e5d109a..dc62005f0 100644 --- a/colossalai/auto_parallel/meta_profiler/meta_registry/activation.py +++ b/colossalai/auto_parallel/meta_profiler/meta_registry/activation.py @@ -49,10 +49,12 @@ def relu_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, Lis # calculate memory cost # NOTE: the inplace ReLU don't have forward memory cost - fwd_memory_cost = MemoryCost(activation=0 if inplace else activation_size(output_tensor), - parameter=0, - temp=0, - buffer=0) + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward + fwd_memory_cost = MemoryCost( + activation=activation_size(input_tensor) if inplace else activation_size([output_tensor, input_tensor]), + parameter=0, + temp=0, + buffer=0) bwd_memory_cost = MemoryCost(activation=activation_size(input_tensor), parameter=0, temp=0, buffer=0) diff --git a/colossalai/auto_parallel/meta_profiler/meta_registry/conv.py b/colossalai/auto_parallel/meta_profiler/meta_registry/conv.py index c7c6beee3..63d6cdc39 100644 --- a/colossalai/auto_parallel/meta_profiler/meta_registry/conv.py +++ b/colossalai/auto_parallel/meta_profiler/meta_registry/conv.py @@ -96,19 +96,19 @@ def convnd_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, L # calculate memory cost # TODO: use profiler to check conv temp memory - fwd_memory_cost = MemoryCost(activation=activation_size(output_tensor), - parameter=activation_size(weight_tensor) + - activation_size(bias_tensor) if has_bias else activation_size(weight_tensor), - temp=0, - buffer=0) + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward + fwd_memory_cost = MemoryCost( + activation=activation_size([input_tensor, output_tensor]), + parameter=activation_size([weight_tensor, bias_tensor]) if has_bias else activation_size(weight_tensor), + temp=0, + buffer=0) - bwd_memory_cost = MemoryCost(activation=activation_size(input_tensor) + activation_size(weight_tensor) + - activation_size(bias_tensor) if has_bias else activation_size(input_tensor) + - activation_size(weight_tensor), - parameter=activation_size(weight_tensor) + - activation_size(bias_tensor) if has_bias else activation_size(weight_tensor), - temp=0, - buffer=0) + bwd_memory_cost = MemoryCost( + activation=activation_size([input_tensor, weight_tensor, bias_tensor]) + if has_bias else activation_size([input_tensor, weight_tensor]), + parameter=activation_size([weight_tensor, bias_tensor]) if has_bias else activation_size(weight_tensor), + temp=0, + buffer=0) # total cost is the sum of forward and backward cost total_cost = MemoryCost(activation=fwd_memory_cost.activation + bwd_memory_cost.activation, diff --git a/colossalai/auto_parallel/meta_profiler/meta_registry/linear.py b/colossalai/auto_parallel/meta_profiler/meta_registry/linear.py index ee42807af..76ed48674 100644 --- a/colossalai/auto_parallel/meta_profiler/meta_registry/linear.py +++ b/colossalai/auto_parallel/meta_profiler/meta_registry/linear.py @@ -106,15 +106,15 @@ def linear_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, L # calculate memory cost # NOTE: Linear don't have buffer and temp in forward and backward phase # the forward activation cost is the size of output_tensor, parameter cost is the size of weight_tensor and bias_tensor - fwd_memory_cost = MemoryCost(activation=activation_size(output_tensor), - parameter=activation_size(weight_tensor) + activation_size(bias_tensor), + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward + fwd_memory_cost = MemoryCost(activation=activation_size([input_tensor, output_tensor]), + parameter=activation_size([weight_tensor, bias_tensor]), temp=0, buffer=0) # the backward activation cost is the size of input_tensor, weight_tensor and bias_tensor, parameter cost is 0 - bwd_memory_cost = MemoryCost(activation=activation_size(input_tensor) + activation_size(weight_tensor) + - activation_size(bias_tensor), - parameter=activation_size(weight_tensor) + activation_size(bias_tensor), + bwd_memory_cost = MemoryCost(activation=activation_size([input_tensor, weight_tensor, bias_tensor]), + parameter=activation_size([weight_tensor, bias_tensor]), temp=0, buffer=0) @@ -142,13 +142,14 @@ def linear_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, L # calculate memory cost # NOTE: Linear don't have buffer and temp in forward and backward phase # the forward activation cost is the size of output_tensor, parameter cost is the size of weight_tensor + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost(activation=activation_size(output_tensor), parameter=activation_size(weight_tensor), temp=0, buffer=0) # the backward activation cost is the size of input_tensor and weight_tensor, parameter cost is 0 - bwd_memory_cost = MemoryCost(activation=activation_size(input_tensor) + activation_size(weight_tensor), + bwd_memory_cost = MemoryCost(activation=activation_size([input_tensor, weight_tensor]), parameter=activation_size(weight_tensor), temp=0, buffer=0) diff --git a/colossalai/auto_parallel/meta_profiler/meta_registry/norm.py b/colossalai/auto_parallel/meta_profiler/meta_registry/norm.py index b3c5924b5..395eecdbb 100644 --- a/colossalai/auto_parallel/meta_profiler/meta_registry/norm.py +++ b/colossalai/auto_parallel/meta_profiler/meta_registry/norm.py @@ -76,7 +76,8 @@ def batchnormnd_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleIt # calculate memory cost # the fwd activation cost is output plus saved mean and saved inv std - fwd_memory_cost = MemoryCost(activation=activation_size([output_tensor, mean_tensor, var_tensor]), + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward + fwd_memory_cost = MemoryCost(activation=activation_size([input_tensor, output_tensor, mean_tensor, var_tensor]), parameter=activation_size([weight_tensor, bias_tensor]), temp=0, buffer=activation_size([mean_tensor, var_tensor])) diff --git a/colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py b/colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py index a77b9c75f..63f321519 100644 --- a/colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py +++ b/colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py @@ -110,7 +110,8 @@ def maxpool_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, # calculate memory cost # NOTE: the index matrix will be discarded in backward phase - fwd_mem_cost = MemoryCost(activation=activation_size(output_tensor) + activation_size(index_matrix)) + # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward + fwd_mem_cost = MemoryCost(activation=activation_size([input_tensor, output_tensor, index_matrix])) # temp memory for backward is the index matrix to be discarded bwd_mem_cost = MemoryCost(activation=activation_size(input_tensor) - activation_size(index_matrix),