<commit_before># Copyright (c) 2017 Sony Corporation. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

import time
import csv
import os
import numpy as np
from importlib import import_module
from collections import OrderedDict
from contextlib import contextmanager

from nnabla.ext_utils import import_extension_module

from .base import FunctionHookCallbackBase


class TimeProfiler(FunctionHookCallbackBase):
    """
    An utility API to create function_hook callbacks to profile the execution time of each function.
    Passing ``ext_name`` and ``device_id``, you can define which device time you want to profile.
    If `ext_name` = "cuda" or "cudnn", then cudaEvent will be used to measure the execution time.
    For more information about cudaEvent, see CUDA docs by NVIDIA corp.(https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html)
    If `ext_name`="cpu" , then wall-clock-time on host will be used.

    Example:

    .. code-block:: python

        ext_name = "cpu"
        device_id = "0"

        from nnabla.ext_utils import get_extension_context
        ctx = get_extension_context(ext_name, device_id=device_id)
        nn.set_default_context(ctx)

        y = model(...)

        from nnabla.utils.inspection import TimeProfiler
        tp = TimeProfiler(ext_name=ext_name, device_id=device_id)

        for i in range(max_iter):
            # All results of executions under "forward" scope are registered as "forward" execution.
            with tp.scope("forward"):
                y.forward(function_pre_hook=tp.pre_hook, function_post_hook=tp.post_hook)<commit_msg>Fix doc in TimeProfiler<commit_after># Copyright (c) 2017 Sony Corporation. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

import time
import csv
import os
import numpy as np
from importlib import import_module
from collections import OrderedDict
from contextlib import contextmanager

from nnabla.ext_utils import import_extension_module

from .base import FunctionHookCallbackBase


class TimeProfiler(FunctionHookCallbackBase):
    """
    An utility API to create function_hook callbacks to profile the execution time of each function.
    Passing ``ext_name`` and ``device_id``, you can define which device time you want to profile.
    If `ext_name` = "cuda" or "cudnn", then cudaEvent will be used to measure the execution time.
    For more information about cudaEvent, see the CUDA `document <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html>`_.
    If `ext_name`="cpu" , then wall-clock-time on host will be used.

    Example:

    .. code-block:: python

        ext_name = "cpu"
        device_id = "0"

        from nnabla.ext_utils import get_extension_context
        ctx = get_extension_context(ext_name, device_id=device_id)
        nn.set_default_context(ctx)

        y = model(...)

        from nnabla.utils.inspection import TimeProfiler
        tp = TimeProfiler(ext_name=ext_name, device_id=device_id)

        for i in range(max_iter):
            # All results of executions under "forward" scope are registered as "forward" execution.
            with tp.scope("forward"):
                y.forward(function_pre_hook=tp.pre_hook, function_post_hook=tp.post_hook)
{'fraction_non_alphanumeric': 0.07141211702372725, 'fraction_numerical': 0.00414651002073255, 'mean_word_length': 3.694054054054054, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5554496', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1187, 'n_words': 463}