- 
                Notifications
    
You must be signed in to change notification settings  - Fork 4.5k
 
[Inference] Add cast and op_functor for cuda build-in types #5546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            Courtesy-Xs
  merged 1 commit into
  hpcaitech:feature/colossal-infer
from
Courtesy-Xs:add_cuda_type_functors
  
      
      
   
  Apr 8, 2024 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #pragma once | ||
| 
     | 
||
| #include <cuda.h> | ||
| #include <cuda_bf16.h> | ||
| #include <cuda_fp16.h> | ||
| #include <cuda_runtime.h> | ||
| 
     | 
||
| #include <functional> | ||
| 
     | 
||
| #include "../utils/micros.h" | ||
| 
     | 
||
| // Note(LiuYang): This file provides base math operation for data type | ||
| // include POD and cuda built-in type such as half and __nv_bfloat16 | ||
| 
     | 
||
| namespace colossalAI { | ||
| namespace cuda { | ||
| namespace funcs { | ||
| 
     | 
||
| // Get type2 from type or vice versa (applied to half and bfloat16) | ||
| template <typename T> | ||
| struct TypeConverter { | ||
| using Type = half2; | ||
| }; // keep for generality | ||
| 
     | 
||
| template <> | ||
| struct TypeConverter<half2> { | ||
| using Type = at::Half; | ||
| }; | ||
| 
     | 
||
| template <> | ||
| struct TypeConverter<at::Half> { | ||
| using Type = half2; | ||
| }; | ||
| 
     | 
||
| template <> | ||
| struct TypeConverter<__nv_bfloat162> { | ||
| using Type = at::BFloat16; | ||
| }; | ||
| 
     | 
||
| template <> | ||
| struct TypeConverter<at::BFloat16> { | ||
| using Type = __nv_bfloat162; | ||
| }; | ||
| 
     | 
||
| template <typename From, typename To> | ||
| struct CastFunctor : public std::unary_function<From, To> { | ||
| HOSTDEVICE To operator()(From val) { return static_cast<To>(val); } | ||
| }; | ||
| 
     | 
||
| #define COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(FROM, TO, STMT, \ | ||
| FUNCTION_MODIFIER) \ | ||
| template <> \ | ||
| struct CastFunctor<FROM, TO> : public std::unary_function<FROM, TO> { \ | ||
| FUNCTION_MODIFIER TO operator()(FROM val) { return STMT; } \ | ||
| }; | ||
| 
     | 
||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(int2, float2, make_float2(val.x, val.y), | ||
| DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(float, float2, make_float2(val, val), | ||
| DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(half2, float2, __half22float2(val), DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(float2, half2, __float22half2_rn(val), | ||
| DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(float, half2, __float2half2_rn(val), | ||
| DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(half, half2, __half2half2(val), DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(half, float, __half2float(val), DEVICE) | ||
| COLOSSAL_CAST_FUNCTOR_SPECIALIZATION(float, nv_bfloat162, | ||
| __float2bfloat162_rn(val), DEVICE) | ||
| 
     | 
||
| #undef COLOSSAL_CAST_FUNCTOR_SPECIALIZATION | ||
| } // namespace funcs | ||
| } // namespace cuda | ||
| } // namespace colossalAI | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.