Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ repos:
# pass_filenames: false
# require_serial: true

- id: check-log-usage
name: Ensure 'llama_stack.log' usage for logging
entry: bash
language: system
types: [python]
pass_filenames: true
args:
- -c
- |
matches=$(grep -EnH '^[^#]*\b(import\s+logging|from\s+logging\b)' "$@" | grep -v -e '#\s*allow-direct-logging' || true)
if [ -n "$matches" ]; then
# GitHub Actions annotation format
while IFS=: read -r file line_num rest; do
echo "::error file=$file,line=$line_num::Do not use 'import logging' or 'from logging import' in $file. Use the custom log instead: from llama_stack.log import get_logger; logger = get_logger(). If direct logging is truly needed, add: # allow-direct-logging"
done <<< "$matches"
exit 1
fi
exit 0

ci:
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
Expand Down
4 changes: 2 additions & 2 deletions llama_stack/core/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# the root directory of this source tree.

import importlib.resources
import logging
import sys

from pydantic import BaseModel
Expand All @@ -17,9 +16,10 @@
from llama_stack.core.utils.exec import run_command
from llama_stack.core.utils.image_types import LlamaStackImageType
from llama_stack.distributions.template import DistributionTemplate
from llama_stack.log import get_logger
from llama_stack.providers.datatypes import Api

log = logging.getLogger(__name__)
log = get_logger(name=__name__, category="core")

# These are the dependencies needed by the distribution server.
# `llama-stack` is automatically installed by the installation script.
Expand Down
4 changes: 2 additions & 2 deletions llama_stack/core/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
import logging
import textwrap
from typing import Any

Expand All @@ -21,9 +20,10 @@
from llama_stack.core.utils.config_dirs import EXTERNAL_PROVIDERS_DIR
from llama_stack.core.utils.dynamic import instantiate_class_type
from llama_stack.core.utils.prompt_for_config import prompt_for_config
from llama_stack.log import get_logger
from llama_stack.providers.datatypes import Api, ProviderSpec

logger = logging.getLogger(__name__)
logger = get_logger(name=__name__, category="core")


def configure_single_provider(registry: dict[str, ProviderSpec], provider: Provider) -> Provider:
Expand Down
5 changes: 3 additions & 2 deletions llama_stack/core/library_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import asyncio
import inspect
import json
import logging
import logging # allow-direct-logging
import os
import sys
from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -48,14 +48,15 @@
from llama_stack.core.utils.config import redact_sensitive_fields
from llama_stack.core.utils.context import preserve_contexts_async_generator
from llama_stack.core.utils.exec import in_notebook
from llama_stack.log import get_logger
from llama_stack.providers.utils.telemetry.tracing import (
CURRENT_TRACE_CONTEXT,
end_trace,
setup_logger,
start_trace,
)

logger = logging.getLogger(__name__)
logger = get_logger(name=__name__, category="core")

T = TypeVar("T")

Expand Down
4 changes: 2 additions & 2 deletions llama_stack/core/request_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import contextvars
import json
import logging
from contextlib import AbstractContextManager
from typing import Any

from llama_stack.core.datatypes import User
from llama_stack.log import get_logger

from .utils.dynamic import instantiate_class_type

log = logging.getLogger(__name__)
log = get_logger(name=__name__, category="core")

# Context variable for request provider data and auth attributes
PROVIDER_DATA_VAR = contextvars.ContextVar("provider_data", default=None)
Expand Down
2 changes: 1 addition & 1 deletion llama_stack/core/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import functools
import inspect
import json
import logging
import logging # allow-direct-logging
import os
import ssl
import sys
Expand Down
6 changes: 3 additions & 3 deletions llama_stack/core/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import logging
import importlib
import os
import signal
import subprocess
import sys

from termcolor import cprint

log = logging.getLogger(__name__)
from llama_stack.log import get_logger

import importlib
log = get_logger(name=__name__, category="core")


def formulate_run_args(image_type: str, image_name: str) -> list:
Expand Down
5 changes: 3 additions & 2 deletions llama_stack/core/utils/prompt_for_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

import inspect
import json
import logging
from enum import Enum
from typing import Annotated, Any, Literal, Union, get_args, get_origin

from pydantic import BaseModel
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefinedType

log = logging.getLogger(__name__)
from llama_stack.log import get_logger

log = get_logger(name=__name__, category="core")


def is_list_of_primitives(field_type):
Expand Down
4 changes: 2 additions & 2 deletions llama_stack/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import logging
import logging # allow-direct-logging
import os
import re
from logging.config import dictConfig
from logging.config import dictConfig # allow-direct-logging

from rich.console import Console
from rich.errors import MarkupError
Expand Down
5 changes: 3 additions & 2 deletions llama_stack/models/llama/llama3/multimodal/encoder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

# Copyright (c) Meta Platforms, Inc. and its affiliates.
import math
from logging import getLogger

import torch
import torch.nn.functional as F

from llama_stack.log import get_logger

from .utils import get_negative_inf_value, to_2tuple

logger = getLogger()
logger = get_logger(name=__name__, category="models::llama")


def resize_local_position_embedding(orig_pos_embed, grid_size):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@

import math
from collections import defaultdict
from logging import getLogger
from typing import Any

import torch
import torchvision.transforms as tv
from PIL import Image
from torchvision.transforms import functional as F

from llama_stack.log import get_logger

IMAGE_RES = 224

logger = getLogger()
logger = get_logger(name=__name__, category="models::llama")


class VariableSizeImageTransform:
Expand Down
9 changes: 5 additions & 4 deletions llama_stack/models/llama/llama3/multimodal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import logging
import math
from collections.abc import Callable
from functools import partial
Expand All @@ -22,6 +20,8 @@
from torch import Tensor, nn
from torch.distributed import _functional_collectives as funcol

from llama_stack.log import get_logger

from ..model import ModelArgs, RMSNorm, apply_rotary_emb, precompute_freqs_cis
from .encoder_utils import (
build_encoder_attention_mask,
Expand All @@ -34,9 +34,10 @@
from .image_transform import VariableSizeImageTransform
from .utils import get_negative_inf_value, to_2tuple

logger = logging.getLogger(__name__)
MP_SCALE = 8

logger = get_logger(name=__name__, category="models")


def reduce_from_tensor_model_parallel_region(input_):
"""All-reduce the input tensor across model parallel group."""
Expand Down Expand Up @@ -771,7 +772,7 @@ def load_hook(
if embed is not None:
# reshape the weights to the correct shape
nt_old, nt_old, _, w = embed.shape
logging.info(f"Resizing tile embedding from {nt_old}x{nt_old} to {self.num_tiles}x{self.num_tiles}")
logger.info(f"Resizing tile embedding from {nt_old}x{nt_old} to {self.num_tiles}x{self.num_tiles}")
embed_new = TilePositionEmbedding._dynamic_resize(embed, self.num_tiles)
# assign the weights to the module
state_dict[prefix + "embedding"] = embed_new
Expand Down
8 changes: 4 additions & 4 deletions llama_stack/models/llama/llama3/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.


from collections.abc import Collection, Iterator, Sequence, Set
from logging import getLogger
from pathlib import Path
from typing import (
Literal,
Expand All @@ -14,11 +14,9 @@

import tiktoken

from llama_stack.log import get_logger
from llama_stack.models.llama.tokenizer_utils import load_bpe_file

logger = getLogger(__name__)


# The tiktoken tokenizer can handle <=400k chars without
# pyo3_runtime.PanicException.
TIKTOKEN_MAX_ENCODE_CHARS = 400_000
Expand All @@ -31,6 +29,8 @@

_INSTANCE = None

logger = get_logger(name=__name__, category="models::llama")


class Tokenizer:
"""
Expand Down
5 changes: 3 additions & 2 deletions llama_stack/models/llama/llama4/quantization/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import logging
import os
from collections.abc import Callable

Expand All @@ -13,11 +12,13 @@
from torch import Tensor, nn
from torch.nn import functional as F

from llama_stack.log import get_logger

from ...datatypes import QuantizationMode
from ..model import Transformer, TransformerBlock
from ..moe import MoE

log = logging.getLogger(__name__)
log = get_logger(name=__name__, category="models")


def swiglu_wrapper_no_reduce(
Expand Down
7 changes: 3 additions & 4 deletions llama_stack/models/llama/llama4/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# the root directory of this source tree.

from collections.abc import Collection, Iterator, Sequence, Set
from logging import getLogger
from pathlib import Path
from typing import (
Literal,
Expand All @@ -14,11 +13,9 @@

import tiktoken

from llama_stack.log import get_logger
from llama_stack.models.llama.tokenizer_utils import load_bpe_file

logger = getLogger(__name__)


# The tiktoken tokenizer can handle <=400k chars without
# pyo3_runtime.PanicException.
TIKTOKEN_MAX_ENCODE_CHARS = 400_000
Expand Down Expand Up @@ -101,6 +98,8 @@ def get_reserved_special_tokens(name, count, start_index=0):
"<|fim_suffix|>",
]

logger = get_logger(name=__name__, category="models::llama")


class Tokenizer:
"""
Expand Down
5 changes: 3 additions & 2 deletions llama_stack/models/llama/quantize_impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

# type: ignore
import collections
import logging

log = logging.getLogger(__name__)
from llama_stack.log import get_logger

log = get_logger(name=__name__, category="llama")

try:
import fbgemm_gpu.experimental.gen_ai # noqa: F401
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import logging
import uuid
from collections.abc import AsyncGenerator
from datetime import UTC, datetime
Expand Down Expand Up @@ -42,6 +41,7 @@
from llama_stack.apis.tools import ToolGroups, ToolRuntime
from llama_stack.apis.vector_io import VectorIO
from llama_stack.core.datatypes import AccessRule
from llama_stack.log import get_logger
from llama_stack.providers.utils.kvstore import InmemoryKVStoreImpl, kvstore_impl
from llama_stack.providers.utils.pagination import paginate_records
from llama_stack.providers.utils.responses.responses_store import ResponsesStore
Expand All @@ -51,7 +51,7 @@
from .persistence import AgentInfo
from .responses.openai_responses import OpenAIResponsesImpl

logger = logging.getLogger()
logger = get_logger(name=__name__, category="agents")


class MetaReferenceAgentsImpl(Agents):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# the root directory of this source tree.

import json
import logging
import uuid
from datetime import UTC, datetime

Expand All @@ -15,9 +14,10 @@
from llama_stack.core.access_control.datatypes import AccessRule
from llama_stack.core.datatypes import User
from llama_stack.core.request_headers import get_authenticated_user
from llama_stack.log import get_logger
from llama_stack.providers.utils.kvstore import KVStore

log = logging.getLogger(__name__)
log = get_logger(name=__name__, category="agents")


class AgentSessionInfo(Session):
Expand Down
Loading
Loading