Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/clamp.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / clamp.cu` 67 lines ยท 2812 bytes Raw Blame History 1 #include "clamp.cuh" 2 3 static __device__ __forceinline__ float op_clamp(float x, float min, float max) { 4 return fminf(fmaxf(x, min), max); 5 } 6 7 // src and dst may be views: rows are contiguous, dims 1..3 follow the strides (in elements). 8 template 9 static __global__ void op_clamp_kernel(const T * x, T * dst, const T min, const T max, const uint32_t k, 10 const uint3 ne0, const uint3 ne1, const uint3 ne2, 11 const uint32_t s01, const uint32_t s02, const uint32_t s03, 12 const uint32_t s1, const uint32_t s2, const uint32_t s3) { 13 const uint32_t i = blockDim.x*blockIdx.x + threadIdx.x; 14 15 if (i >= k) { 16 return; 17 } 18 19 const uint2 d0 = fast_div_modulo(i, ne0); // 20 const uint2 d1 = fast_div_modulo(d0.x, ne1); // 21 const uint2 d2 = fast_div_modulo(d1.x, ne2); // 22 23 const size_t i_src = d0.y + size_t(d1.y)*s01 + size_t(d2.y)*s02 + size_t(d2.x)*s03; 24 const size_t i_dst = d0.y + size_t(d1.y)*s1 + size_t(d2.y)*s2 + size_t(d2.x)*s3; 25 26 dst[i_dst] = (T)op_clamp((float)x[i_src], (float)min, (float)max); 27 } 28 29 template 30 static void clamp_cuda(const T * x, T * dst, const T min, const T max, const ggml_tensor * src0, const ggml_tensor * t, cudaStream_t stream) { 31 const int64_t k = ggml_nelements(src0); 32 const size_t ts = sizeof(T); 33 GGML_ASSERT(k <= std::numeric_limits::max()); 34 35 const uint3 ne0 = init_fastdiv_values(src0->ne[0]); 36 const uint3 ne1 = init_fastdiv_values(src0->ne[1]); 37 const uint3 ne2 = init_fastdiv_values(src0->ne[2]); 38 39 const int64_t num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE; 40 op_clamp_kernel<<>>(x, dst, min, max, (uint32_t) k, ne0, ne1, ne2, 41 src0->nb[1]/ts, src0->nb[2]/ts, src0->nb[3]/ts, 42 t->nb[1]/ts, t->nb[2]/ts, t->nb[3]/ts); 43 } 44 45 46 void ggml_cuda_op_clamp(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 47 const ggml_tensor * src0 = dst->src[0]; 48 const void * src0_d = src0->data; 49 void * dst_d = dst->data; 50 cudaStream_t stream = ctx.stream(); 51 52 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); 53 GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); 54 GGML_ASSERT(src0->type == dst->type); 55 GGML_ASSERT(ggml_is_contiguous_rows(src0) && ggml_is_contiguous_rows(dst)); 56 57 float min; 58 float max; 59 memcpy(&min, dst->op_params, sizeof(float)); 60 memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); 61 62 if (src0->type == GGML_TYPE_F16) { 63 clamp_cuda((const half *)src0_d, (half *)dst_d, (half)min, (half)max, src0, dst, stream); 64 } else { 65 clamp_cuda((const float *)src0_d, (float *)dst_d, (float)min, (float)max, src0, dst, stream); 66 } 67 }