Git Product home page Git Product logo

globaler's Introduction

Nested Timer

The timer can be nested and includes metadata.

Install

git clone https://github.com/zejia-lin/globaler
pip install .

Quick Start

Simple Usage

from globaler import Timer

timer = Timer()

timer.start("End to end")
timer.start("work_1", metadata={"m": 1024, "n": 512, "k": 2048})
work_1()
timer.stop()

timer.start("work_2")
work_2()
timer.stop()
timer.stop() # stops the end to end timer

Global Timer

There is a global timer to facilitate timing in many files.

from globaler import getTimer()
timer = getTimer()

Getting Results

The Timer class includes several ways to convert the results.

  • to_json/save_json: returns a nested json.
  • to_csv/save_csv: returns a dict that can be passed to pd.Dataframe(timer.to_csv())
  • to_dataframe: returns a pandas dataframe.
  • to_list/save_list: returns a plain list.

Examples:

# Json
print(timer.to_json())
timer.save_json("result.json")

# CSV
print(timer.to_csv())
print(timer.to_dataframe())
timer.save_csv("results.csv")

Synchronize

GPU tasks like torch may have to call torch.cuda.synchronize() before stopping the timer.

import torch
import torch.nn.functional as F

X = torch.randn((2048, 4096), device='cuda')
W = torch.randn((5120, 4096), device='cuda')
Y = F.linear(X, W) # warmup

timer = Timer()
timer.start("without sync")
Y = F.linear(X, W)
timer.stop()

timer.start("with sync")
Y = F.linear(X, W)
timer.stop(torch.cuda)

Synchronizing CUDA stream.

stream = torch.cuda.Stream()
with torch.cuda.stream(stream):
    timer.start("stream")
    Y = F.linear(X, W)
    timer.stop(stream)

Possible output, the start and end are Unix timestamps created by python's time.time().

   id          name         start           end  duration          metadata
0   1  without sync  1.723192e+09  1.723192e+09  0.000340              None
1   2     with sync  1.723192e+09  1.723192e+09  0.110201              None
2   3        stream  1.723192e+09  1.723192e+09  0.110122              None

globaler's People

Contributors

zejia-lin avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.