Skip to content

Add tool to collect compilation statistics - #9227

Merged
pzread merged 11 commits into
iree-org:mainfrom
pzread:compilation-stats
Jun 10, 2022
Merged

pzread merged 11 commits into
iree-org:mainfrom
pzread:compilation-stats

Conversation

@pzread

@pzread pzread commented May 26, 2022 •

Copy link
Copy Markdown
Contributor

Inspired by #9167, this tool parses the zip info of vmfb and the ninja build log to get the binary size (dispatch libraries) and the module compilation time.

There are 4 sizes to be retrieved:

  1. Module file size
  2. VM bytecode size in the module.
  3. Const binary size in the module.
  4. Total size of all dispatch libraries in the module.

The class BenchmarkCase now also parses the model name into the name and tags.

@pzread
pzread force-pushed the compilation-stats branch 2 times, most recently from 55ac611 to c80a276 Compare May 26, 2022 18:45
@pzread
pzread force-pushed the compilation-stats branch 5 times, most recently from 88bb279 to cf1c7c6 Compare June 2, 2022 13:42
@pzread
pzread force-pushed the compilation-stats branch from cf1c7c6 to d3f069c Compare June 6, 2022 13:39
@pzread pzread added the infrastructure/benchmark Relating to benchmarking infrastructure label Jun 6, 2022
@pzread
pzread force-pushed the compilation-stats branch from d3f069c to 15056e4 Compare June 6, 2022 13:43
@pzread
pzread marked this pull request as ready for review June 6, 2022 13:48
@pzread
pzread requested a review from dcaballe June 6, 2022 13:48
@iree-github-actions-bot

Copy link
Copy Markdown
Contributor

Comment on lines +92 to +99
if identified_names != set(size_map.keys()):
raise AssertionError(
f"Unrecognized components in the module: {size_map.keys()}.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This shouldn't be an assertion error, since it comes from parsing external input.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to RuntimeException.

Comment on lines +116 to +118
if not module_path:
raise AssertionError(
f"Can't find the module file in the flagfile: {flagfile_path}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same. This should not be an AssertionError. Also the AssertionError in main below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't find a good built-in exception, use the generic RuntimeException instead.

Comment on lines +466 to +472
return {
"model_name": self.model_name,
"model_tags": self.model_tags,
"model_source": self.model_source,
"target_arch": self.target_arch,
"bench_mode": self.bench_mode,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you can use asdict, which will also work for recursive dataclasses. I think ultimately the right way to do this is to use a custom json encoder/decoder rather than having extra methods on each dataclass

Suggested change
return {
"model_name": self.model_name,
"model_tags": self.model_tags,
"model_source": self.model_source,
"target_arch": self.target_arch,
"bench_mode": self.bench_mode,
}
return self.asdict()

Again, with this replacement, it's not clear that the extra method is really worth it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

asdict() can recursively serialize the dataclass. So I removed all to_json_object() methods and just let caller to serialize it.

@pzread
pzread force-pushed the compilation-stats branch from efb526a to d5ab645 Compare June 6, 2022 19:29
@pzread
pzread requested a review from GMNGeoffrey June 6, 2022 19:31
@pzread
pzread force-pushed the compilation-stats branch from d5ab645 to 79d1bff Compare June 9, 2022 16:22
@pzread
pzread force-pushed the compilation-stats branch from 79d1bff to f149165 Compare June 9, 2022 16:23
@pzread
pzread force-pushed the compilation-stats branch from 8a968a3 to f88266c Compare June 9, 2022 16:31
@pzread

pzread commented Jun 9, 2022

Copy link
Copy Markdown
Contributor Author

Kindly ping : )

@dcaballe

dcaballe commented Jun 9, 2022

Copy link
Copy Markdown
Contributor

I don't think I can help much here. I'll leave it to @GMNGeoffrey and @antiagainst :)

@pzread
pzread merged commit 6d41cd0 into iree-org:main Jun 10, 2022
Comment on lines +84 to +86
with zipfile.ZipFile(module) as module_zipfile:
size_map = dict(
(info.filename, info.file_size) for info in module_zipfile.infolist())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The file size of binaries often has surprisingly coarse granularity and tracking just that may fail to observe incremental code size increase, until some threshold is reached and the file size finally bumps. In my experience (and that is what we have converged on in internal google CI for code size regression tests), something that works reasonably well is size -A <thefile>, and then look at the last line, Total. Here is an example. Consider these two small C++ shared libraries, foo and bar:

benoitjacob@desk:~$ cat foo.cc 
#include <unordered_map>

int foo(int a, int b) {
  std::unordered_map<int, int> m;
  m[a] = b;
  m[b] = a;
  return m[a] + m[b];
}

benoitjacob@desk:~$ cat bar.cc
#include <unordered_map>

int foo(int a, int b) {
  std::unordered_map<int, int> m;
  m[a] = b;
  m[b] = a;
  m[a + b] = a - b;
  m[a - b] = a + b;
  if (m.count(123)) return 456;
  return m[a] + m[b];
}

benoitjacob@desk:~$ clang++ foo.cc -g0 -O2 -shared -fPIC -o /tmp/foo.so
benoitjacob@desk:~$ clang++ bar.cc -g0 -O2 -shared -fPIC -o /tmp/bar.so
benoitjacob@desk:~$ ls -l /tmp/{foo,bar}.so
-rwxr-xr-x 1 benoitjacob primarygroup 17232 Jun 10 10:11 /tmp/bar.so
-rwxr-xr-x 1 benoitjacob primarygroup 17232 Jun 10 10:11 /tmp/foo.so

So both libraries, foo.so and bar.so, have the same file size, 17232 bytes, despite bar doing a bit more work.

Fortunately, size can see the difference:

benoitjacob@desk:~$ size -A /tmp/foo.so | grep Total
Total                5966
benoitjacob@desk:~$ size -A /tmp/bar.so | grep Total
Total                6602

This is a > 10% code size increase!

@pzread pzread Jun 10, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! @bjacob , sorry I didn't pay enough attention to it.

I was thinking that we might also have similar issues on other payloads (E.g., SPIR-V flatbuffer), so I preferred not to do ad-hoc things for dynamic libraries first. I'll spend some time checking the better ways to do this #9436.

Just tried to think about the cause of the difference, I guess that might be due to the alignment of the ELF section, so each section is padded to the page size.

@bjacob bjacob Jun 10, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👍
agree it looks like some kind of alignment. but in my experience, it's often more than page size. i seem to remember it was 64k in internal google android aarch64 builds of TFlite. It might depend on things like relocations, which can create a use for having some extra space in the file (presumably the reason why the file itself matters is that it gets memory-mapped when the binary is loaded, so the file layout becomes the memory layout, and then the binary loader still needs to perform relocations on that). If it really is relocations that are the issue, then IREE should be less affected than TFLite, though.

jpienaar pushed a commit to jpienaar/iree that referenced this pull request Jun 10, 2022
@GMNGeoffrey GMNGeoffrey added the infrastructure Relating to build systems, CI, or testing label Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure/benchmark Relating to benchmarking infrastructure infrastructure Relating to build systems, CI, or testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants