GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Loading models directly into VRAM, norm calculation on GPUs, broadcasting for ggml_mul by JohannesGaessler · Pull Request #1483 · ggml-org/llama.cpp

Loading models directly into VRAM, norm calculation on GPUs, broadcasting for ggml_mul #1483

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.

Sign up for GitHub

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
ggerganov merged 35 commits into ggml-org:master from JohannesGaessler:gpu-norms
May 20, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

Loading models directly into VRAM, norm calculation on GPUs, broadcasting for ggml_mul #1483

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
de65783
Broadcasting for ggml_mul
JohannesGaessler May 16, 2023
2365a2a
CUDA kernel for ggml_mul, norms in VRAM
JohannesGaessler May 16, 2023
fa1a29f
GPU weights not in RAM, direct loading with cuFile
JohannesGaessler May 17, 2023
1bfe5a9
fixup! GPU weights not in RAM, direct loading with cuFile
JohannesGaessler May 18, 2023
24d5ddf
fixup! GPU weights not in RAM, direct loading with cuFile
JohannesGaessler May 19, 2023
09d8251
define default model path once, sync path with readme (#1366)
ott2 May 16, 2023
230018d
~7% faster Q5_1 AVX2 code (#1477)
ilyakurdyukov May 16, 2023
1af2844
convert.py: Support models which are stored in a single pytorch_model…
TheBloke May 16, 2023
d5207bf
benchmark-matmul: Print the average of the test results (#1490)
May 17, 2023
d916c5b
Remove unused n_parts parameter (#1509)
sw May 17, 2023
a94b334
Fixes #1511 lambda issue for w64devkit (mingw) (#1513)
DannyDaemonic May 18, 2023
e22541a
make kv_f16 the default for api users (#1517)
Green-Sky May 18, 2023
6b5776b
minor : fix compile warnings
ggerganov May 19, 2023
75c017f
readme : adds WizardLM to the list of supported models (#1485)
n1trane May 19, 2023
c51c64a
main : make reverse prompt option act as a stop token in non-interact…
data-angel May 19, 2023
0226d49
examples : add persistent chat (#1495)
ejones May 19, 2023
9fd8187
tests : add missing header
ggerganov May 19, 2023
211aa6a
ggml : use F16 instead of F32 in Q4_0, Q4_1, Q8_0 (#1508)
ggerganov May 19, 2023
9a7af6c
ggml : fix scalar implementation of Q4_1 dot
ggerganov May 20, 2023
f14673a
llama : fix compile warnings in llama_set_state_data()
ggerganov May 20, 2023
df512bb
llama : fix name shadowing and C4146 (#1526)
maximegmd May 20, 2023
f401d5f
Fix for mingw (#1462)
DannyDaemonic May 20, 2023
54ec8a9
llama : add llama_init_backend() API (close #1527)
ggerganov May 20, 2023
667c57f
feature : add blis and other BLAS implementation support (#1502)
zenixls2 May 20, 2023
977e74d
Revert "feature : add blis and other BLAS implementation support (#15…
ggerganov May 20, 2023
ffe9652
GPU weights not in RAM, direct loading with cuFile
JohannesGaessler May 17, 2023
f67bc3c
llama : code style fixes + progress print fix
ggerganov May 20, 2023
3ec7941
ggml : ggml_mul better broadcast support
ggerganov May 20, 2023
a3586c5
cmake : workarounds for cufile when CMake version < 3.25
ggerganov May 20, 2023
909acb3
Merge branch 'master' into gpu-norms
ggerganov May 20, 2023
fee87f6
gg rebase fixup
JohannesGaessler May 20, 2023
b81f662
Loop in llama.cpp, fixed progress callback
JohannesGaessler May 20, 2023
fadcd58
Attempt clang-tidy fix
JohannesGaessler May 20, 2023
a4da072
llama : fix vram size computation
ggerganov May 20, 2023
37f2c6c
Add forgotten fclose()
JohannesGaessler May 20, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
ggml : ggml_mul better broadcast support
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@ggerganov
ggerganov committed May 20, 2023
commit 3ec7941bad08c39bc2fc113e9ec1b27351e33f12

There are no files selected for viewing

84 changes: 50 additions & 34 deletions 84 ggml.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,12 @@ static inline bool ggml_can_repeat(const struct ggml_tensor * t0, const struct g
(t1->ne[3]%t0->ne[3] == 0);
}

static inline bool ggml_can_repeat_rows(const struct ggml_tensor * t0, const struct ggml_tensor * t1) {
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");

return (t0->ne[0] == t1->ne[0]) && ggml_can_repeat(t0, t1);
}

static inline int ggml_up32(int n) {
return (n + 31) & ~31;
}
Expand Down Expand Up @@ -4658,11 +4664,15 @@ struct ggml_tensor * ggml_mul_impl(
struct ggml_tensor * a,
struct ggml_tensor * b,
bool inplace) {
GGML_ASSERT(a->ne[0] == b->ne[0] && ggml_can_repeat(b, a));
// TODO: support less-strict constraint
// GGML_ASSERT(ggml_can_repeat(b, a));
GGML_ASSERT(ggml_can_repeat_rows(b, a));

bool is_node = false;

if (!inplace && (a->grad || b->grad)) {
// TODO: support backward pass for broadcasting
GGML_ASSERT(ggml_are_same_shape(a, b));
is_node = true;
}

Expand Down Expand Up @@ -7960,22 +7970,14 @@ static void ggml_compute_forward_mul_f32(
const struct ggml_tensor * src0,
const struct ggml_tensor * src1,
struct ggml_tensor * dst) {
const int nr = ggml_nrows(src0);
const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne10 = src1->ne[0];
const int64_t ne11 = src1->ne[1];
const int64_t ne12 = src1->ne[2];
const int64_t ne13 = src0->ne[3];

GGML_ASSERT(ne00 == ne10 && ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
GGML_ASSERT(ggml_can_repeat_rows(src1, src0) && ggml_are_same_shape(src0, dst));

if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
return;
}
const int ith = params->ith;
const int nth = params->nth;

#ifdef GGML_USE_CUBLAS
if (src1->backend == GGML_BACKEND_CUDA) {
if (ith == 0) {
Expand All @@ -7985,6 +7987,17 @@ static void ggml_compute_forward_mul_f32(
}
#endif

const int64_t nr = ggml_nrows(src0);

const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];

const int64_t ne10 = src1->ne[0];
const int64_t ne11 = src1->ne[1];
const int64_t ne12 = src1->ne[2];
const int64_t ne13 = src1->ne[3];

const size_t nb00 = src0->nb[0];
const size_t nb01 = src0->nb[1];
const size_t nb02 = src0->nb[2];
Expand All @@ -8002,47 +8015,50 @@ static void ggml_compute_forward_mul_f32(

GGML_ASSERT( nb0 == sizeof(float));
GGML_ASSERT(nb00 == sizeof(float));
GGML_ASSERT(ne00 == ne10);

if (nb10 == sizeof(float) && ggml_are_same_shape(src0, src1)) {
for (int ir = ith; ir < nr; ir += nth) {
// src0, src1 and dst are same shape => same indices
const int i3 = ir/(ne02*ne01);
const int i2 = (ir - i3*ne02*ne01)/ne01;
const int i1 = (ir - i3*ne02*ne01 - i2*ne01);
if (nb10 == sizeof(float)) {
for (int64_t ir = ith; ir < nr; ir += nth) {
// src0 and dst are same shape => same indices
const int64_t i03 = ir/(ne02*ne01);
const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);

const int64_t i13 = i03 % ne13;
const int64_t i12 = i02 % ne12;
const int64_t i11 = i01 % ne11;

float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);

#ifdef GGML_USE_ACCELERATE
UNUSED(ggml_vec_mul_f32);

vDSP_vmul(
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1,
(float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1,
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1,
ne00);
vDSP_vmul( src0_ptr, 1, src1_ptr, 1, dst_ptr, 1, ne00);
#else
ggml_vec_mul_f32(ne00,
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
(float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
ggml_vec_mul_f32(ne00, dst_ptr, src0_ptr, src1_ptr);
#endif
// }
// }
}
} else {
// src1 is not contiguous
for (int ir = ith; ir < nr; ir += nth) {
for (int64_t ir = ith; ir < nr; ir += nth) {
// src0 and dst are same shape => same indices
// src1 is broadcastable across src0 and dst in i1, i2, i3
const int i03 = ir/(ne02*ne01);
const int i02 = (ir - i03*ne02*ne01)/ne01;
const int i01 = (ir - i03*ne02*ne01 - i02*ne01);
const int i13 = i03 % ne13;
const int i12 = i02 % ne12;
const int i11 = i01 % ne11;
const int64_t i03 = ir/(ne02*ne01);
const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);

const int64_t i13 = i03 % ne13;
const int64_t i12 = i02 % ne12;
const int64_t i11 = i01 % ne11;

float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
for (int i0 = 0; i0 < ne00; i0++) {

for (int64_t i0 = 0; i0 < ne00; i0++) {
float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i0*nb10);

dst_ptr[i0] = src0_ptr[i0] * (*src1_ptr);
Expand Down
32 changes: 10 additions & 22 deletions 32 llama.cpp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,12 @@ static void llama_model_load_internal(
}
}

#ifdef GGML_USE_CUBLAS
#define LLAMA_BACKEND_OFFLOAD GGML_BACKEND_CUDA
#else
#define LLAMA_BACKEND_OFFLOAD GGML_BACKEND_CPU
#endif

// prepare memory for the weights
size_t vram_total = 0;
{
Expand All @@ -1016,19 +1022,19 @@ static void llama_model_load_internal(
{
ggml_backend backend_output;
if (n_gpu_layers > int(n_layer)) {
Comment thread
ggerganov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Outdated

@github-actions github-actions Bot May 20, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: if with identical then and else branches [bugprone-branch-clone]

            if (n_gpu_layers > int(n_layer)) {
            ^
Additional context

llama.cpp:1024: else branch starts here

            } else {
              ^

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Outdated

@github-actions github-actions Bot May 20, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: if with identical then and else branches [bugprone-branch-clone]

            if (n_gpu_layers > int(n_layer)) {
            ^
Additional context

llama.cpp:1022: else branch starts here

            } else {
              ^

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
backend_output = GGML_BACKEND_CUDA;
backend_output = LLAMA_BACKEND_OFFLOAD;
} else {
backend_output = GGML_BACKEND_CPU;
}

model.output = ml->get_tensor("output.weight", {n_embd, n_vocab}, backend_output);
model.output = ml->get_tensor("output.weight", {n_embd, n_vocab}, backend_output);
}

const int i_gpu_start = n_layer - n_gpu_layers;

model.layers.resize(n_layer);
for (uint32_t i = 0; i < n_layer; ++i) {
const ggml_backend backend = int(i) < i_gpu_start ? GGML_BACKEND_CPU : GGML_BACKEND_CUDA;
const ggml_backend backend = int(i) < i_gpu_start ? GGML_BACKEND_CPU : LLAMA_BACKEND_OFFLOAD;
Comment thread
ggerganov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


auto & layer = model.layers[i];

Expand All @@ -1047,7 +1053,7 @@ static void llama_model_load_internal(
layer.w2 = ml->get_tensor(layers_i + ".feed_forward.w2.weight", { n_ff, n_embd}, backend);
layer.w3 = ml->get_tensor(layers_i + ".feed_forward.w3.weight", {n_embd, n_ff}, backend);

if (backend == GGML_BACKEND_CUDA) {
if (backend == LLAMA_BACKEND_OFFLOAD) {
vram_total +=
ggml_nbytes(layer.attention_norm) + ggml_nbytes(layer.wq) + ggml_nbytes(layer.wk) +
ggml_nbytes(layer.wv) + ggml_nbytes(layer.wo) + ggml_nbytes(layer.attention_norm) +
Expand Down Expand Up @@ -1213,13 +1219,7 @@ static bool llama_eval_internal(
cur = ggml_rms_norm(ctx0, inpL);

// cur = cur*attention_norm(broadcasted)
#ifdef GGML_USE_CUBLAS
cur = ggml_mul(ctx0, cur, model.layers[il].attention_norm);
#else
cur = ggml_mul(ctx0,
ggml_repeat(ctx0, model.layers[il].attention_norm, cur),
cur);
#endif
}

// self-attention
Expand Down Expand Up @@ -1327,13 +1327,7 @@ static bool llama_eval_internal(
cur = ggml_rms_norm(ctx0, inpFF);

// cur = cur*ffn_norm(broadcasted)
#ifdef GGML_USE_CUBLAS
cur = ggml_mul(ctx0, cur, model.layers[il].ffn_norm);
#else
cur = ggml_mul(ctx0,
ggml_repeat(ctx0, model.layers[il].ffn_norm, cur),
cur);
#endif
}

struct ggml_tensor * tmp = ggml_mul_mat(ctx0,
Expand Down Expand Up @@ -1371,13 +1365,7 @@ static bool llama_eval_internal(
inpL = ggml_rms_norm(ctx0, inpL);

// inpL = inpL*norm(broadcasted)
#ifdef GGML_USE_CUBLAS
inpL = ggml_mul(ctx0, inpL, model.norm);
#else
inpL = ggml_mul(ctx0,
ggml_repeat(ctx0, model.norm, inpL),
inpL);
#endif

embeddings = inpL;
}
Expand Down
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.
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.