Skip to content

Commit

Permalink
Add first report
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Feb 25, 2024
1 parent 7d0b91a commit 202dbc0
Show file tree
Hide file tree
Showing 5 changed files with 9,973 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@
*.dbg
/gemma
/.aarch64
/trace
15 changes: 15 additions & 0 deletions gemma.cc
Expand Up @@ -64,6 +64,12 @@
#include "src/sentencepiece_processor.h"
// #include "third_party/sentencepiece/src/util.h"

#include "justine.h"

int g_run_count;
int g_eval_count;
int g_matvec_count;

namespace gcpp {

template <class TConfig>
Expand Down Expand Up @@ -546,6 +552,9 @@ void GenerateImpl(GemmaImpl<TConfig>& gemma, const InferenceArgs& args,
pos_offset += end_offset;
}

g_matvec_count = 0;
++g_eval_count;

if (verbosity >= 2) {
// in the future this output should not occur in GenerateImpl but instead
// should be available as observable state for frontend code to handle I/O.
Expand Down Expand Up @@ -587,6 +596,8 @@ void GenerateImpl(GemmaImpl<TConfig>& gemma, const InferenceArgs& args,
token = SampleTopK<kTopK>(activations.logits.data(), kVocabSize, gen,
args.temperature, accept_token);
}
g_matvec_count = 0;
++g_eval_count;
if (!stream_token(token, activations.logits[token])) {
token = EOS_ID;
}
Expand Down Expand Up @@ -754,6 +765,7 @@ void GemmaImpl<ConfigGemma2B>::Generate(const InferenceArgs& args,
(*this, args, prompt, start_pos, pool, inner_pool, stream_token, accept_token,
gen, verbosity);
}

template <>
void GemmaImpl<ConfigGemma7B>::Generate(const InferenceArgs& args,
const std::vector<int>& prompt,
Expand Down Expand Up @@ -797,6 +809,9 @@ void GenerateGemma(Gemma& gemma, const InferenceArgs& args,
gemma.impl_->Generate(args, prompt, start_pos, pool, inner_pool, stream_token,
accept_token, gen, verbosity);
pool.SetWaitMode(hwy::PoolWaitMode::kBlock);
g_matvec_count = 0;
g_eval_count = 0;
++g_run_count;
}

} // namespace gcpp
Expand Down
11 changes: 11 additions & 0 deletions justine.h
@@ -0,0 +1,11 @@
#ifndef JUSTINE_H_
#define JUSTINE_H_
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

extern int g_run_count;
extern int g_eval_count;
extern int g_matvec_count;

#endif
47 changes: 47 additions & 0 deletions ops.h
Expand Up @@ -48,6 +48,8 @@
#include "hwy/contrib/math/math-inl.h"
#include "hwy/contrib/matvec/matvec-inl.h"

#include "justine.h"

HWY_BEFORE_NAMESPACE();
namespace gcpp {
namespace HWY_NAMESPACE {
Expand Down Expand Up @@ -196,6 +198,51 @@ HWY_INLINE void MatVec(const CompressedArray<MatT, kCapacity>& mat,
detail::FullDotProductsForStrip(df, mat, mat_ofs, kInner, r0, num_rows,
vec_aligned, out + r0);
}

#if 0

char path[64];
snprintf(path, sizeof(path), "trace/%03d_%03d_%05d.dat", g_run_count, g_eval_count, g_matvec_count++);
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);
write(fd, out, kOuter * 4);
close(fd);

#else

char path[64];
snprintf(path, sizeof(path), "trace/%03d_%03d_%05d.dat", g_run_count, g_eval_count, g_matvec_count++);
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
fprintf(stderr, "%s: not found\n", path);
return;
}
ssize_t len;
len = lseek(fd, 0, SEEK_END);
if (len != kOuter * 4) {
fprintf(stderr, "%s: unexpected length: wanted %ld but got %ld\n", path, (long)(kOuter * 4), (long)len);
return;
}
float *gold = (float *)malloc(len);
if (pread(fd, gold, len, 0) != len) {
fprintf(stderr, "%s: read failed: %s\n", path, strerror(errno));
return;
}
close(fd);
double sad = 0;
float min[2] = {__FLT_MAX__, __FLT_MAX__};
float max[2] = {__FLT_MIN__, __FLT_MIN__};
for (int i = 0; i < kOuter; ++i) {
sad += std::abs(out[i] - gold[i]);
min[0] = std::min(min[0], gold[i]);
min[1] = std::min(min[1], out[i]);
max[0] = std::max(max[0], gold[i]);
max[1] = std::max(max[1], out[i]);
}
free(gold);
sad /= kOuter;
fprintf(stderr, "%s: sad %g [gold %g .. %g] [out %g .. %g]\n", path, sad, min[0], max[0], min[1], max[1]);

#endif
}

template <class D, HWY_IF_F32_D(D)>
Expand Down

0 comments on commit 202dbc0

Please sign in to comment.