Skip to content

Commit 225bf8c

Browse files
committed
CUDA: add bilinear interpolation for upscale
1 parent 67d1ef2 commit 225bf8c

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

ggml/include/ggml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ extern "C" {
495495
GGML_OP_POOL_1D,
496496
GGML_OP_POOL_2D,
497497
GGML_OP_POOL_2D_BACK,
498-
GGML_OP_UPSCALE, // nearest interpolate
498+
GGML_OP_UPSCALE,
499499
GGML_OP_PAD,
500500
GGML_OP_PAD_REFLECT_1D,
501501
GGML_OP_ROLL,

ggml/src/ggml-cuda/ggml-cuda.cu

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,6 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
33733373
case GGML_OP_GROUP_NORM:
33743374
return ggml_is_contiguous(op->src[0]);
33753375
case GGML_OP_UPSCALE:
3376-
return op->src[0]->type == GGML_TYPE_F32 && op->op_params[0] == GGML_SCALE_MODE_NEAREST;
33773376
case GGML_OP_PAD:
33783377
case GGML_OP_ARANGE:
33793378
case GGML_OP_TIMESTEP_EMBEDDING:

ggml/src/ggml-cuda/upscale.cu

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,88 @@ static __global__ void upscale_f32(const float * x, float * dst,
2222
dst[index] = *( (const float *)((const char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00) );
2323
}
2424

25+
static __global__ void upscale_f32_bilinear(const float * x, float * dst,
26+
const int nb00, const int nb01, const int nb02, const int nb03,
27+
const int ne00_src, const int ne01_src,
28+
const int ne10_dst, const int ne11_dst, const int ne12_dst, const int ne13_dst,
29+
const float sf0, const float sf1, const float sf2, const float sf3,
30+
const float pixel_offset) {
31+
int index = threadIdx.x + blockIdx.x * blockDim.x;
32+
int dst_total_elements = ne10_dst * ne11_dst * ne12_dst * ne13_dst;
33+
34+
if (index >= dst_total_elements) {
35+
return;
36+
}
37+
38+
int i10_dst = index % ne10_dst;
39+
int i11_dst = (index / ne10_dst) % ne11_dst;
40+
int i12_dst = (index / (ne10_dst * ne11_dst)) % ne12_dst;
41+
int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst);
42+
43+
int i02_src = (int)(i12_dst / sf2);
44+
int i03_src = (int)(i13_dst / sf3);
45+
46+
float y_src_f = ((float)i11_dst + pixel_offset) / sf1 - pixel_offset;
47+
int y0_src = (int)floorf(y_src_f);
48+
int y1_src = y0_src + 1;
49+
50+
y0_src = max(0, min(y0_src, ne01_src - 1));
51+
y1_src = max(0, min(y1_src, ne01_src - 1));
52+
53+
float dy = y_src_f - (float)y0_src;
54+
dy = max(0.0f, min(dy, 1.0f));
55+
56+
float x_src_f = ((float)i10_dst + pixel_offset) / sf0 - pixel_offset;
57+
int x0_src = (int)floorf(x_src_f);
58+
int x1_src = x0_src + 1;
59+
60+
x0_src = max(0, min(x0_src, ne00_src - 1));
61+
x1_src = max(0, min(x1_src, ne00_src - 1));
62+
63+
float dx = x_src_f - (float)x0_src;
64+
dx = max(0.0f, min(dx, 1.0f));
65+
66+
const float * p_a = (const float *)((const char *)x + (long)x0_src * nb00 + (long)y0_src * nb01 + (long)i02_src * nb02 + (long)i03_src * nb03);
67+
const float * p_b = (const float *)((const char *)x + (long)x1_src * nb00 + (long)y0_src * nb01 + (long)i02_src * nb02 + (long)i03_src * nb03);
68+
const float * p_c = (const float *)((const char *)x + (long)x0_src * nb00 + (long)y1_src * nb01 + (long)i02_src * nb02 + (long)i03_src * nb03);
69+
const float * p_d = (const float *)((const char *)x + (long)x1_src * nb00 + (long)y1_src * nb01 + (long)i02_src * nb02 + (long)i03_src * nb03);
70+
71+
const float val_a = *p_a;
72+
const float val_b = *p_b;
73+
const float val_c = *p_c;
74+
const float val_d = *p_d;
75+
76+
float result = val_a * (1.0f - dx) * (1.0f - dy) +
77+
val_b * dx * (1.0f - dy) +
78+
val_c * (1.0f - dx) * dy +
79+
val_d * dx * dy;
80+
81+
dst[index] = result;
82+
}
83+
2584
static void upscale_f32_cuda(const float * x, float * dst,
2685
const int nb00, const int nb01, const int nb02, const int nb03,
2786
const int ne10, const int ne11, const int ne12, const int ne13,
2887
const float sf0, const float sf1, const float sf2, const float sf3,
2988
cudaStream_t stream) {
30-
int dst_size = ne10 * ne11 * ne12 * ne13;
89+
int dst_size = ne10 * ne11 * ne12 * ne13;
3190
int num_blocks = (dst_size + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
3291

3392
upscale_f32<<<num_blocks, CUDA_UPSCALE_BLOCK_SIZE,0,stream>>>(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3);
3493
}
3594

95+
static void upscale_f32_bilinear_cuda(const float * x, float * dst,
96+
const int nb00, const int nb01, const int nb02, const int nb03,
97+
const int ne00_src, const int ne01_src,
98+
const int ne10_dst, const int ne11_dst, const int ne12_dst, const int ne13_dst,
99+
const float sf0, const float sf1, const float sf2, const float sf3,
100+
const float pixel_offset, cudaStream_t stream) {
101+
int dst_size = ne10_dst * ne11_dst * ne12_dst * ne13_dst;
102+
int num_blocks = (dst_size + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
103+
104+
upscale_f32_bilinear<<<num_blocks, CUDA_UPSCALE_BLOCK_SIZE,0,stream>>>(x, dst, nb00, nb01, nb02, nb03, ne00_src, ne01_src, ne10_dst, ne11_dst, ne12_dst, ne13_dst, sf0, sf1, sf2, sf3, pixel_offset);
105+
}
106+
36107
void ggml_cuda_op_upscale(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
37108
const ggml_tensor * src0 = dst->src[0];
38109
const float * src0_d = (const float *)src0->data;
@@ -42,10 +113,25 @@ void ggml_cuda_op_upscale(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
42113
GGML_ASSERT(src0->type == GGML_TYPE_F32);
43114
GGML_ASSERT( dst->type == GGML_TYPE_F32);
44115

45-
const float sf0 = (float)dst->ne[0]/src0->ne[0];
46-
const float sf1 = (float)dst->ne[1]/src0->ne[1];
47-
const float sf2 = (float)dst->ne[2]/src0->ne[2];
48-
const float sf3 = (float)dst->ne[3]/src0->ne[3];
116+
const int mode_flags = dst->op_params[0];
117+
const ggml_scale_mode mode = (ggml_scale_mode)(mode_flags & 0xFF);
118+
119+
float sf0 = (float)dst->ne[0]/src0->ne[0];
120+
float sf1 = (float)dst->ne[1]/src0->ne[1];
121+
float sf2 = (float)dst->ne[2]/src0->ne[2];
122+
float sf3 = (float)dst->ne[3]/src0->ne[3];
49123

50-
upscale_f32_cuda(src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3, stream);
124+
if (mode == GGML_SCALE_MODE_NEAREST) {
125+
upscale_f32_cuda(src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3, stream);
126+
} else if (mode == GGML_SCALE_MODE_BILINEAR) {
127+
float pixel_offset = 0.5f;
128+
if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) {
129+
sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
130+
sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
131+
pixel_offset = 0.0f;
132+
}
133+
upscale_f32_bilinear_cuda(src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
134+
src0->ne[0], src0->ne[1], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
135+
sf0, sf1, sf2, sf3, pixel_offset, stream);
136+
}
51137
}

0 commit comments

Comments
 (0)