Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:visualDust/neetbox into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
visualDust committed Nov 21, 2023
2 parents bffbacd + 988acae commit 45d9517
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 80 deletions.
6 changes: 3 additions & 3 deletions neetbox/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
# URL: https://gong.host
# Date: 20230413

import collections
from multiprocessing import current_process

DEFAULT_GLOBAL_CONFIG = {
"daemon": {
"enable": True,
"allowIpython": False,
"servers": [{"host": "localhost", "port": "20202"},],
"servers": [
{"host": "localhost", "port": "20202"},
],
"mode": "detached",
"displayName": None,
"uploadInterval": 10,
Expand Down
2 changes: 1 addition & 1 deletion neetbox/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __new__(cls, name: str) -> "Registry":

# not compatible with python below 3.8
def __init__(self, name, *args, **kwargs) -> None:
if not "initialized" in self:
if "initialized" not in self:
self["initialized"] = True
self.name = name

Expand Down
2 changes: 0 additions & 2 deletions neetbox/daemon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# Date: 20230414

import json
import os
import platform
import subprocess
import time

Expand Down
11 changes: 3 additions & 8 deletions neetbox/daemon/_client_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
# Date: 20230414


from neetbox.config import get_module_level_config
from neetbox.daemon._local_http_client import _local_http_client
from neetbox.logging import logger
from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback

module_name = get_frame_module_traceback().__name__
module_name = get_frame_module_traceback().__name__ # type: ignore
assert pkg.is_installed(
"httpx", try_install_if_not=True
), f"{module_name} requires httpx which is not installed"
import json
import time

import httpx

from neetbox.config import get_module_level_config
from neetbox.logging import logger

logger = logger("NEETBOX DAEMON API")

Expand Down
16 changes: 8 additions & 8 deletions neetbox/daemon/_daemon_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import time
from threading import Thread
from typing import Union

from neetbox.config import get_module_level_config
from neetbox.daemon._local_http_client import _local_http_client
Expand All @@ -15,14 +16,13 @@

__TIME_UNIT_SEC = 0.1

__upload_thread: Thread = None
__upload_thread: Union[Thread, None] = None


def _upload_thread(daemon_config, base_addr, display_name):
_ctr = 0
_api_name = "sync"
_api_addr = f"{base_addr}/{_api_name}/{display_name}"
global _update_value_dict
_disconnect_flag = False
_disconnect_retries = 10
while True:
Expand All @@ -36,18 +36,18 @@ def _upload_thread(daemon_config, base_addr, display_name):
_headers = {"Content-Type": "application/json"}
try:
# upload data
resp = _local_http_client.post(_api_addr, data=_data, headers=_headers)
if resp.is_error: # upload failed
resp = _local_http_client.post(_api_addr, json=_data, headers=_headers)
if resp.is_error: # upload failed
raise IOError(f"Failed to upload data to daemon. ({resp.status_code})")
except Exception as e:
if _disconnect_flag: # already in retries
if _disconnect_flag: # already in retries
_disconnect_retries -= 1
if not _disconnect_retries: # retry count down exceeded
if not _disconnect_retries: # retry count down exceeded
logger.err(
"Failed to reconnect to daemon after {10} retries, Trying to launch new daemon..."
)
from neetbox.daemon import _try_attach_daemon

_try_attach_daemon()
time.sleep(__TIME_UNIT_SEC)
continue
Expand All @@ -58,7 +58,7 @@ def _upload_thread(daemon_config, base_addr, display_name):
else:
if not _disconnect_flag:
continue
logger.ok(f"Successfully reconnected to daemon.")
logger.ok("Successfully reconnected to daemon.")
_disconnect_flag = False
_disconnect_retries = 10

Expand Down
6 changes: 2 additions & 4 deletions neetbox/daemon/_daemon_launcher.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
print("========= Daemon Launcher =========")

import argparse
import json
import os
import sys

from neetbox.daemon._flask_server import daemon_process

# sys.stdout=open(r'D:\Projects\ML\neetbox\logdir\daemon.log', 'a+')


# from neetbox.daemon._local_http_client import _local_http_client
print("========= Daemon Launcher =========")


def run():
Expand All @@ -26,6 +25,5 @@ def run():


print("_daemon_launcher is starting with __name__ =", __name__, " and args =", sys.argv)

run()
print("_daemon_launcher: exiting")
7 changes: 7 additions & 0 deletions neetbox/daemon/_flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from flask import Flask, abort, json, request

from neetbox.config import get_module_level_config
from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback

module_name = get_frame_module_traceback().__name__ # type: ignore
assert pkg.is_installed(
"flask", try_install_if_not=True
), f"{module_name} requires flask which is not installed"

_STAT_POOL = {}
__DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC = 60 * 60 * 12 # 12 Hours
Expand Down
7 changes: 4 additions & 3 deletions neetbox/daemon/_local_http_client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import httpx
import logging

import httpx

httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.ERROR)

__no_proxy = {
"http://": None,
"https://": None,
}


def __load_http_client():
__local_http_client = httpx.Client(proxies=__no_proxy)
__local_http_client = httpx.Client(proxies=__no_proxy) # type: ignore
return __local_http_client


Expand Down
5 changes: 2 additions & 3 deletions neetbox/daemon/daemonable_process.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import multiprocessing
import os
import subprocess
import sys
import time
from typing import *
from typing import List, Literal

is_ms_windows = "win32" in sys.platform or "cygwin" in sys.platform

Expand Down Expand Up @@ -59,7 +58,7 @@ def start(self):
creationflags = {
"attached": 0,
"shared": 0,
"detached": subprocess.CREATE_NO_WINDOW,
"detached": subprocess.CREATE_NO_WINDOW, # type: ignore (only for windows)
}[self.__mode]

popen = subprocess.Popen(
Expand Down
3 changes: 1 addition & 2 deletions neetbox/integrations/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from functools import partial
from random import random
from threading import Event
from typing import Any, Dict, Iterable, List, Union
from typing import Dict, List, Union
from urllib.request import urlopen

import numpy as np
Expand All @@ -25,7 +25,6 @@
TextColumn,
TimeRemainingColumn,
TransferSpeedColumn,
track,
)

from neetbox.integrations import engine
Expand Down
6 changes: 1 addition & 5 deletions neetbox/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

_cfg = get_module_level_config()
logger.set_log_dir(_cfg["logdir"])
from neetbox.logging.logger import (
LogMetadata,
LogSplitStrategies,
SplitStrategyCallable,
)
from neetbox.logging.logger import LogSplitStrategies

__all__ = ["logger", "LogSplitStrategies"]
5 changes: 2 additions & 3 deletions neetbox/logging/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
# Date: 20230318

from dataclasses import dataclass
import os
import warnings
from random import random
from typing import Optional


@dataclass
class LogStyle:
color: Optional[str] = None
Expand All @@ -22,7 +21,7 @@ class LogStyle:
split_char_cmd = " > "
split_char_identity = "/"
split_char_txt = " | "

@classmethod
def get_supported_colors(cls):
return ["red", "green", "blue", "cyan", "yellow", "magenta"]
Expand Down
12 changes: 4 additions & 8 deletions neetbox/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ def catch(
not isclass(exception_type) or not issubclass(exception_type, BaseException)
):
return self.catch()(exception_type)
logger = self

class Catcher:
def __init__(self, from_decorator):
Expand All @@ -415,8 +414,6 @@ def __exit__(self, type_, value, traceback_):
return
if not issubclass(type_, exception_type):
return False
from_decorator = self._from_decorator
catch_options = [(type_, value, traceback_)]
if handler:
handler(traceback_)
# logger.log(
Expand Down Expand Up @@ -456,19 +453,18 @@ def catch_wrapper(*args, **kwargs):
return catch_wrapper

return Catcher(False)

def mention(self, func):
@functools.wraps(func)
def with_logging(*args, **kwargs):
self.log(f"Currently running: {func.__name__}",traceback=3)
self.log(f"Currently running: {func.__name__}", traceback=3)
return func(*args, **kwargs)

return with_logging

def banner(self, text, font: Optional[str] = None):
from pyfiglet import Figlet, FigletFont

from neetbox.utils import pkg

builtin_font_list = [
"ansiregular",
"ansishadow",
Expand Down Expand Up @@ -575,7 +571,7 @@ def _bind_file(self, path):
return self

def file_bend(self) -> bool:
return self.file_writer != None
return self.file_writer is not None


DEFAULT_LOGGER = Logger(None)
1 change: 0 additions & 1 deletion neetbox/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# URL: https://gong.host
# Date: 20230417

from neetbox.pipeline._pipe import Pipe
from neetbox.pipeline._signal_and_slot import listen, watch

__all__ = ["watch", "listen"]
3 changes: 0 additions & 3 deletions neetbox/pipeline/_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
# Date: 20230413

from neetbox.core import Registry
from neetbox.logging import logger
from neetbox.utils import pkg
from neetbox.utils.mvc import Singleton

pipes = Registry("__pipe")

Expand Down
4 changes: 0 additions & 4 deletions neetbox/torch/arch/cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
# URL: https://gong.host
# Date: 20230315

import torch
import torch.nn as nn
import torch.nn.functional as F

from neetbox.utils import pkg


def ConvNxN(
Expand Down
2 changes: 0 additions & 2 deletions neetbox/torch/arch/mask_boundary_finder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import torch
import torch.nn.functional as F

from neetbox.utils import pkg


class MaskEdgeDetecter(torch.nn.Module):
"""generate the 'edge bar' for a 0-1 mask Groundtruth of a image
Expand Down
2 changes: 1 addition & 1 deletion neetbox/torch/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def profile(
f"Seems your model has an imbalanced performance peek between CUDA side synchronous test and none-sync one. Consider raising speedtest loop times (currently {speedtest} +2) to have a stable result."
)
logger.debug(
f"Note that the CUDA side synchronous speedtest is more reliable since you are using a GPU."
"Note that the CUDA side synchronous speedtest is more reliable since you are using a GPU."
)
if profiling:
if speedtest:
Expand Down
2 changes: 1 addition & 1 deletion neetbox/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from neetbox.utils._package import PipPackageHealper as pkg
from neetbox.utils._package import pipPackageHealper as pkg

__all__ = ["pkg"]
6 changes: 3 additions & 3 deletions neetbox/utils/_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def install(self, package, terminate=False):
_installed = False
while retry:
if not retry:
error_str = f"Bad Input"
error_str = "Bad Input"
raise ValueError(error_str)
print(f"{caller_name} want to install {package} via pip.")
choice = input("Make your choice: [y]/n")
Expand Down Expand Up @@ -55,7 +55,7 @@ def is_installed(self, package: str, try_install_if_not: Union[str, bool] = True
importlib.import_module(package)
self.installed_packages.append(package)
return True
except:
except: # noqa
package_name_install = (
package if type(try_install_if_not) is bool else try_install_if_not
)
Expand All @@ -68,4 +68,4 @@ def is_installed(self, package: str, try_install_if_not: Union[str, bool] = True


# singleton
PipPackageHealper = PipPackageHealper()
pipPackageHealper = PipPackageHealper()
Loading

0 comments on commit 45d9517

Please sign in to comment.