Finding and Using Models

This document follows the 1_finding_and_using_models.ipynb tutorial.

hwcomponents supports many different component models. We can list available component models with the get_models() function. This function returns a list of EnergyAreaModel subclasses.

You may also use the hwcomponents --list command from the shell.

import hwcomponents as hwc

# Find and list all installed component models
print(f'Listing component models')
all_installed_models = hwc.get_models()
print(f'Number of available component models: {len(all_installed_models)}')
for model in all_installed_models[:5]:
    print(f'{model.__name__} supports {model.component_name}')

If we know what type of component we would like to model, we can use the name_must_include argument to get_models() to find all models that match a given class name.

For example, we can use the hwcomponents_cacti package to model an SRAM. Once we’ve found the model, we can use the help function to see its documentation and supported actions.


# Find an SRAM model
print(f'\nListing SRAM models')
sram_models = hwc.get_models(name_must_include='sram')
print(f'Number of SRAM models: {len(sram_models)}')
for model in sram_models[:5]:
    print(f'{model.__name__} supports {model.component_name}')

# Grab the CACI SRAM model & use the "help" function to see its documentation
models = [s for s in sram_models if "hwcomponents_cacti" in s.__module__]
assert len(models) == 1, \
    f"Excected 1 CACTI SRAM model, got {len(models)}. Is hwcomponents_cacti installed?"
sram = models[0]
help(sram)

Once we know the model we’d like to use, we can import the model directly and instantiate components.


# Now that we have a component model, we can use it to create components.
from hwcomponents_cacti.hwcomponents_cacti import SRAM

# Create an SRAM component using the CACTI model
sram = SRAM(
    tech_node=16e-9,
    width=1024,
    depth=256,
)
print(f'SRAM read energy: {sram.read(bits_per_action=8)} J')
print(f'SRAM write energy: {sram.write(bits_per_action=8)} J')
print(f'SRAM area: {sram.area} m^2')
print(f'SRAM leak power: {sram.leak_power} W')

If you’re unsure of which component model you’d like to use, there are other ways to invoke a model. There are three ways to find a component model:

  1. Import the model from a module and use it directly.

  2. Ask hwcomponents to select the best model for a given component. hwcomponents will select the best model for a given component name and attributes, and raise an error if no model can be instantiated with the given attributes.

  3. Ask for specific properties from hwcomponents. This is similar to the second method, but you can ask for the energy, area, or leakage power of a component directly.


# Method 1: Import the model from a module and use it directly.
from hwcomponents_cacti import SRAM
sram = SRAM(
    tech_node=40e-9, # 40nm
    width=64,
    depth=1024,
)
print(f"SRAM read energy is {sram.read():.2e}J. Area is {sram.area:.2e}m^2. Leak power is {sram.leak_power:.2e}W")


# Method 2: Ask hwcomponents to select the best model for a given component.
model = hwc.get_model(
    component_name="SRAM", # These are NOT case sensitive.
    component_attributes={
        "tech_node": 40e-9, # 40nm
        "width": 64,
        "depth": 1024
    },
    required_actions=["read"]
)
print(f'Read energy is {model.read():.2e}J. Area is {model.area:.2e}m^2. Leak power is {model.leak_power:.2e}W')

# Method 3: Ask for specific properties from hwcomponents
attributes = {
    "tech_node": 40e-9, # 40nm
    "width": 64,
    "depth": 1024
}

read_energy = hwc.get_energy(
    component_name="SRAM",
    component_attributes=attributes,
    action_name="read",
    action_arguments={}
)
area = hwc.get_area(
    component_name="SRAM",
    component_attributes=attributes,
)
leak_power = hwc.get_leak_power(
    component_name="SRAM",
    component_attributes=attributes,
)
print(f'Read energy is {read_energy:.2e}J. Area is {area:.2e}m^2. Leak power is {leak_power:.2e}W')