linux-hd.c

/*/*
*  linux/kernel/hd.c
*
*  (C) 1991  Linus Torvalds
*/
/*
* This is the low-level hd interrupt support. It traverses the
* request-list, using interrupts to jump between functions. As
* all the functions are called within interrupts, we may not
* sleep. Special care is recommended.
*
*  modified by Drew Eckhardt to check nr of hd's from the CMOS.
*/
#include <linux/config.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/hdreg.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/segment.h>
#define MAJOR_NR 3
#include "blk.h"
#define CMOS_READ(addr) ({ /                    //--读CMOS中硬盘信息
outb_p(0x80|addr,0x70); /
inb_p(0x71); /
})
/* Max read/write errors/sector */
#define MAX_ERRORS    7
#define MAX_HD        2
static void recal_intr(void);
static void bad_rw_intr(void);
static int recalibrate = 0;
static int reset = 0;
/*
*  This struct defines the HD's and their types.
*/
struct hd_i_struct {                            //--定义硬盘参数及类型
    int head,sect,cyl,wpcom,lzone,ctl;
    };
#ifdef HD_TYPE
struct hd_i_struct hd_info[] = { HD_TYPE };
#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))
#else
struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
static int NR_HD = 0;
#endif
static struct hd_struct {
    long start_sect;
    long nr_sects;
} hd[5*MAX_HD]={{0,0},};
static int hd_sizes[5*MAX_HD] = {0, };
#define port_read(port,buf,nr) /                //--读端口嵌入汇编宏
__asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di")
#define port_write(port,buf,nr) /                //--写端口嵌入汇编宏
__asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si")
extern void hd_interrupt(void);                    //--硬盘中断过程
extern void rd_load(void);                        //--虚拟盘创建加载函数
/* This may be used only once, enforced by 'static int callable' */
int sys_setup(void * BIOS)                        //--系统设置函数,只在初始化时被调用一次
{                //--BIOS为指向硬盘参数表结构的指针
    static int callable = 1;
    int i,drive;
    unsigned char cmos_disks;
    struct partition *p;
    struct buffer_head * bh;
    if (!callable)                                //--只调用一次
        return -1;
    callable = 0;
#ifndef HD_TYPE
    for (drive=0 ; drive<2 ; drive++) {
        hd_info[drive].cyl = *(unsigned short *) BIOS;            //--柱面数
        hd_info[drive].head = *(unsigned char *) (2+BIOS);        //--磁头数
        hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);    //--写前预补偿柱面号
        hd_info[drive].ctl = *(unsigned char *) (8+BIOS);        //--控制字节
        hd_info[drive].lzone = *(unsigned short *) (12+BIOS);    //--磁头着陆区柱面号
        hd_info[drive].sect = *(unsigned char *) (14+BIOS);        //--每磁道扇区数
        BIOS += 16;                                //--每个硬盘参数表长16个字节,这里指向下一表
    }
    if (hd_info[1].cyl)                            //--如果没有第二个硬盘,则信息为空
        NR_HD=2;
    else
        NR_HD=1;
#endif
    for (i=0 ; i<NR_HD ; i++) {                    //--设置硬盘分区结构数组hd[]
        hd[i*5].start_sect = 0;
        hd[i*5].nr_sects = hd_info[i].head*
                hd_info[i].sect*hd_info[i].cyl;
    }
    /*
        We querry CMOS about hard disks : it could be that
        we have a SCSI/ESDI/etc controller that is BIOS
        compatable with ST-506, and thus showing up in our
        BIOS table, but not register compatable, and therefore
        not present in CMOS.
        Furthurmore, we will assume that our ST-506 drives
        <if any> are the primary drives in the system, and
        the ones reflected as drive 1 or 2.
        The first drive is stored in the high nibble of CMOS
        byte 0x12, the second in the low nibble.  This will be
        either a 4 bit drive type or 0xf indicating use byte 0x19
        for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
        Needless to say, a non-zero value means we have
        an AT controller hard disk for that drive.
        
    */
    if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)    //--检测硬盘是不是AT控制器兼容的。
        if (cmos_disks & 0x0f)
            NR_HD = 2;
        else
            NR_HD = 1;
    else
        NR_HD = 0;
    for (i = NR_HD ; i < 2 ; i++) {
        hd[i*5].start_sect = 0;
        hd[i*5].nr_sects = 0;
    }                                            //--到此最终确定硬盘数量
    for (drive=0 ; drive<NR_HD ; drive++) {        //--读取硬盘上第一个扇区的分区表信息,来设置分区结构数组hd[]
        if (!(bh = bread(0x300 + drive*5,0))) {    //--bread()读取硬盘第一个数据块
            printk("Unable to read partition table of drive %d/n/r",
                drive);
            panic("");
        }
        if (bh->b_data[510] != 0x55 || (unsigned char)
            bh->b_data[511] != 0xAA) {
            printk("Bad partition table on drive %d/n/r",drive);
            panic("");
        }
        p = 0x1BE + (void *)bh->b_data;
        for (i=1;i<5;i++,p++) {
            hd[i+5*drive].start_sect = p->start_sect;
            hd[i+5*drive].nr_sects = p->nr_sects;
        }
        brelse(bh);                                //--释放为存放硬盘数据块而申请的缓冲区
    }
    for (i=0 ; i<5*MAX_HD ; i++)                //--对每个分区中数据块进行统计并保存
        hd_sizes[i] = hd[i].nr_sects>>1 ;
    blk_size[MAJOR_NR] = hd_sizes;
    if (NR_HD)
        printk("Partition table%s ok./n/r",(NR_HD>1)?"s":"");
    rd_load();
    init_swapping();
    mount_root();
    return (0);
}
static int controller_ready(void)                //--判断并循环等待硬盘控制器就绪
{
    int retries = 100000;
    while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40);
    return (retries);
}
static int win_result(void)                        //--检测硬盘执行命令后的状态
{
    int i=inb_p(HD_STATUS);
    if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT))
        == (READY_STAT | SEEK_STAT))
        return(0); /* ok */
    if (i&1) i=inb(HD_ERROR);
    return (1);
}
static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
        unsigned int head,unsigned int cyl,unsigned int cmd,
        void (*intr_addr)(void))                //--向硬盘控制器发送命令块
{            //--在硬盘控制器就绪之后,先设置全局函数指针变量do_hd指向硬盘中断处理程序中将会调用的C处理函数
    register int port asm("dx");    //--然后再发送硬盘控制字节和7字节的参数命令块。
    if (drive>1 || head>15)
        panic("Trying to write bad sector");
    if (!controller_ready())
        panic("HD controller not ready");
    SET_INTR(intr_addr);                    //--do_hd = intr_addr
    outb_p(hd_info[drive].ctl,HD_CMD);
    port=HD_DATA;
    outb_p(hd_info[drive].wpcom>>2,++port);
    outb_p(nsect,++port);
    outb_p(sect,++port);
    outb_p(cyl,++port);
    outb_p(cyl>>8,++port);
    outb_p(0xA0|(drive<<4)|head,++port);
    outb(cmd,++port);
}
static int drive_busy(void)                    //--等待硬盘就绪
{
    unsigned int i;
    unsigned char c;
    for (i = 0; i < 50000; i++) {            //--循环50000次等待HD_STATUS
        c = inb_p(HD_STATUS);
        c &= (BUSY_STAT | READY_STAT | SEEK_STAT);
        if (c == (READY_STAT | SEEK_STAT))
            return 0;
    }
    printk("HD controller times out/n/r");
    return(1);
}
static void reset_controller(void)            //--重新校正硬盘控制器
{
    int    i;
    outb(4,HD_CMD);
    for(i = 0; i < 1000; i++) nop();
    outb(hd_info[0].ctl & 0x0f ,HD_CMD);
    if (drive_busy())
        printk("HD-controller still busy/n/r");
    if ((i = inb(HD_ERROR)) != 1)
        printk("HD-controller reset failed: %02x/n/r",i);
}
static void reset_hd(void)                    //--硬盘复位操作
{
    static int i;
repeat:
    if (reset) {
        reset = 0;
        i = -1;
        reset_controller();
    } else if (win_result()) {
        bad_rw_intr();
        if (reset)
            goto repeat;
    }
    i++;
    if (i < NR_HD) {
        hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
            hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
    } else
        do_hd_request();
}
void unexpected_hd_interrupt(void)            //--意外硬盘中断处理函数
{
    printk("Unexpected HD interrupt/n/r");
    reset = 1;
    do_hd_request();
}
static void bad_rw_intr(void)                //--读写硬盘失败处理函数
{
    if (++CURRENT->errors >= MAX_ERRORS)
        end_request(0);
    if (CURRENT->errors > MAX_ERRORS/2)
        reset = 1;
}
static void read_intr(void)                    //--读操作中断调用函数
{                                            //--硬盘读命令hd_out()结束时引发的硬盘中断过程中被调用
    if (win_result()) {
        bad_rw_intr();
        do_hd_request();
        return;
    }
    port_read(HD_DATA,CURRENT->buffer,256);    //--读数据到请求结构缓冲区
    CURRENT->errors = 0;
    CURRENT->buffer += 512;
    CURRENT->sector++;
    if (--CURRENT->nr_sectors) {
        SET_INTR(&read_intr);
        return;
    }
    end_request(1);
    do_hd_request();
}
static void write_intr(void)                //--写扇区中断调用函数
{
    if (win_result()) {
        bad_rw_intr();
        do_hd_request();
        return;
    }
    if (--CURRENT->nr_sectors) {
        CURRENT->sector++;
        CURRENT->buffer += 512;
        SET_INTR(&write_intr);
        port_write(HD_DATA,CURRENT->buffer,256);
        return;
    }
    end_request(1);
    do_hd_request();
}
static void recal_intr(void)                //--硬盘重新校正中断函数
{
    if (win_result())
        bad_rw_intr();
    do_hd_request();
}
void hd_times_out(void)                        //--硬盘操作超时处理
{                                            //--在kernel/sched.c line340中被调用
    if (!CURRENT)
        return;
    printk("HD timeout");
    if (++CURRENT->errors >= MAX_ERRORS)
        end_request(0);
    SET_INTR(NULL);
    reset = 1;
    do_hd_request();
}
void do_hd_request(void)                        //--执行硬盘读写请求操作
{
    int i,r;
    unsigned int block,dev;
    unsigned int sec,head,cyl;
    unsigned int nsect;
    INIT_REQUEST;
    dev = MINOR(CURRENT->dev);                    //--检测请求的合法性
    block = CURRENT->sector;
    if (dev >= 5*NR_HD || block+2 > hd[dev].nr_sects) {
        end_request(0);
        goto repeat;
    }
    block += hd[dev].start_sect;
    dev /= 5;                            //--根据求得的绝对扇区号block和硬盘号dev,计算磁道中的扇区号sec,柱面号syl,磁头号head.
    __asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0),
        "r" (hd_info[dev].sect));
    __asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0),
        "r" (hd_info[dev].head));
    sec++;
    nsect = CURRENT->nr_sectors;
    if (reset) {                        //--向硬盘控制器发送i/o操作信息
        recalibrate = 1;
        reset_hd();
        return;
    }
    if (recalibrate) {
        recalibrate = 0;
        hd_out(dev,hd_info[CURRENT_DEV].sect,0,0,0,
            WIN_RESTORE,&recal_intr);
        return;
    }    
    if (CURRENT->cmd == WRITE) {
        hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
        for(i=0 ; i<10000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++)
            /* nothing */ ;
        if (!r) {
            bad_rw_intr();
            goto repeat;
        }
        port_write(HD_DATA,CURRENT->buffer,256);
    } else if (CURRENT->cmd == READ) {
        hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
    } else
        panic("unknown hd-command");
}
void hd_init(void)                        //--硬盘系统初始化
{
    blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
    set_intr_gate(0x2E,&hd_interrupt);
    outb_p(inb_p(0x21)&0xfb,0x21);
    outb(inb_p(0xA1)&0xbf,0xA1);
}
|xGv00|fcc8d4de8197f69fde70263fb4d52380
(DepMamba) dw@node01:~/DepMamba-main$ conda uninstall pytorch torchvision torchaudio Channels: - defaults - conda-forge - nvidia - pytorch Platform: linux-64 Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: /home/dw/anaconda3/envs/DepMamba removed specs: - pytorch - torchaudio - torchvision The following packages will be REMOVED: alsa-lib-1.2.14-h5eee18b_0 aom-3.9.1-hac33072_0 attr-2.5.1-h166bdaf_1 blas-1.0-mkl brotlicffi-1.0.9.2-py310h6a678d5_1 cairo-1.18.4-h3394656_0 cffi-1.17.1-py310h1fdaa30_1 charset-normalizer-3.3.2-pyhd3eb1b0_0 dav1d-1.2.1-h5eee18b_0 dbus-1.13.18-hb2f20db_0 expat-2.7.1-h6a678d5_0 ffmpeg-7.1.1-gpl_h0cf71c1_707 filelock-3.17.0-py310h06a4308_0 font-ttf-dejavu-sans-mono-2.37-hd3eb1b0_0 font-ttf-inconsolata-2.001-hcb22688_0 font-ttf-source-code-pro-2.030-hd3eb1b0_0 font-ttf-ubuntu-0.83-h8b1ccd4_0 fontconfig-2.15.0-h7e30c49_1 fonts-anaconda-1-h8fa9717_0 fonts-conda-ecosystem-1-hd3eb1b0_0 freetype-2.13.3-ha770c72_1 fribidi-1.0.10-h7b6447c_0 gdk-pixbuf-2.42.12-hb9ae30d_0 gettext-0.25.1-h3f43e3d_1 gettext-tools-0.25.1-h3f43e3d_1 glib-2.84.2-h6287aef_0 glib-tools-2.84.2-h4833e2c_0 gmp-6.3.0-h6a678d5_0 gmpy2-2.2.1-py310h5eee18b_0 graphite2-1.3.14-h295c915_1 harfbuzz-11.3.3-hbb57e21_0 idna-3.7-py310h06a4308_0 intel-openmp-2025.0.0-h06a4308_1171 jinja2-3.1.6-py310h06a4308_0 lame-3.100-h7b6447c_0 lcms2-2.17-h717163a_0 lerc-4.0.0-h6a678d5_0 level-zero-1.24.0-hb700be7_0 libabseil-20250512.1-cxx17_hba17884_0 libasprintf-0.25.1-h3f43e3d_1 libasprintf-devel-0.25.1-h3f43e3d_1 libass-0.17.4-h96ad9f0_0 libcap-2.75-h39aace5_0 libdeflate-1.23-h86f0d12_0 libdrm-2.4.124-h5eee18b_0 libegl-1.7.0-h5eee18b_2 libflac-1.4.3-h59595ed_0 libfreetype-2.13.3-ha770c72_1 libfreetype6-2.13.3-h48d6fc4_1 libgcrypt-lib-1.11.1-hb9d3cd8_0 libgettextpo-0.25.1-h3f43e3d_1 libgettextpo-devel-0.25.1-h3f43e3d_1 libgl-1.7.0-h5eee18b_2 libglib-2.84.2-h3618099_0 libglvnd-1.7.0-h5eee18b_2 libglx-1.7.0-h5eee18b_2 libgpg-error-1.55-h3f2d84a_0 libiconv-1.18-h4ce23a2_1 libjpeg-turbo-3.0.3-h5eee18b_0 libogg-1.3.5-h27cfd23_1 libopenvino-2025.2.0-hb617929_1 libopenvino-auto-batch-plugin-2025.2.0-hed573e4_1 libopenvino-auto-plugin-2025.2.0-hed573e4_1 libopenvino-hetero-plugin-2025.2.0-hd41364c_1 libopenvino-intel-cpu-plugin-2025.2.0-hb617929_1 libopenvino-intel-gpu-plugin-2025.2.0-hb617929_1 libopenvino-intel-npu-plugin-2025.2.0-hb617929_1 libopenvino-ir-frontend-2025.2.0-hd41364c_1 libopenvino-onnx-frontend-2025.2.0-h1862bb8_1 libopenvino-paddle-frontend-2025.2.0-h1862bb8_1 libopenvino-pytorch-frontend-2025.2.0-hecca717_1 libopenvino-tensorflow-frontend-2025.2.0-h0767aad_1 libopenvino-tensorflow-lite-frontend-2025.2.0-hecca717_1 libopus-1.5.2-hd0c01bc_0 libpciaccess-0.18-h5eee18b_0 libpng-1.6.50-h943b412_0 libprotobuf-6.31.1-h9ef548d_1 librsvg-2.58.4-he92a37e_3 libsndfile-1.2.2-hc60ed4a_1 libsystemd0-257.7-h4e0b6ca_0 libtiff-4.7.0-hd9ff511_3 libudev1-257.7-hbe16f8c_0 libunwind-1.6.2-h9c3ff4c_0 liburing-2.9-h84d6215_0 libusb-1.0.29-h73b1eb8_0 libva-2.22.0-h4f16b4b_2 libvorbis-1.3.7-h7b6447c_0 libvpx-1.14.1-hac33072_0 libwebp-base-1.6.0-hd42ef1d_0 libxcb-1.17.0-h9b100fa_0 libxkbcommon-1.10.0-h65c71a3_0 libxml2-2.13.8-h4bc477f_0 llvm-openmp-15.0.7-h0cdce71_0 lz4-c-1.10.0-h5888daf_1 markupsafe-3.0.2-py310h5eee18b_0 mkl-2025.0.0-hacee8c2_941 mkl-service-2.4.0-py310h5eee18b_3 mkl_fft-1.3.11-py310hacdc0fc_1 mkl_random-1.2.8-py310h2fd27a0_1 mpc-1.3.1-h5eee18b_0 mpfr-4.2.1-h5eee18b_0 mpg123-1.32.9-hc50e24c_0 mpmath-1.3.0-py310h06a4308_0 networkx-3.2.1-py310h06a4308_0 numpy-1.21.6-py310h64c44e4_2 numpy-base-1.21.6-py310he1678cf_2 openh264-2.6.0-he621ea3_0 openjpeg-2.5.3-h5fbd93e_0 packaging-25.0-py310h06a4308_0 pango-1.56.4-hadf4263_0 pcre2-10.45-hc749103_0 pillow-11.1.0-py310h7e6dc6c_0 pixman-0.46.4-h537e5f6_0 pthread-stubs-0.3-h0ce48e5_1 pugixml-1.15-h3f63f65_0 pulseaudio-client-17.0-hac146a9_1 pycparser-2.21-pyhd3eb1b0_0 pysocks-1.7.1-py310h06a4308_0 python_abi-3.10-8_cp310 pytorch-2.1.0-py3.10_cuda12.1_cudnn8.9.2_0 pytorch-mutex-1.0-cuda pyyaml-6.0.2-py310h5eee18b_0 requests-2.32.4-py310h06a4308_0 sdl2-2.32.54-h3f2d84a_0 sdl3-3.2.14-he3e324a_0 snappy-1.2.2-h03e3b7b_0 svt-av1-3.0.2-h5888daf_0 sympy-1.13.3-py310h06a4308_1 tbb-2022.0.0-hdb19cb5_0 tbb-devel-2022.0.0-hdb19cb5_0 torchaudio-2.1.0-py310_cu121 torchtriton-2.1.0-py310 torchvision-0.16.0-py310_cu121 typing_extensions-4.12.2-py310h06a4308_0 urllib3-2.5.0-py310h06a4308_0 wayland-1.24.0-hdac8c69_0 wayland-protocols-1.45-hd8ed1ab_0 x264-1!164.3095-h166bdaf_2 x265-3.5-h924138e_3 xkeyboard-config-2.44-h5eee18b_0 xorg-libice-1.1.2-h9b100fa_0 xorg-libsm-1.2.6-he73a12e_0 xorg-libx11-1.8.12-h9b100fa_1 xorg-libxau-1.0.12-h9b100fa_0 xorg-libxcursor-1.2.3-h9b100fa_0 xorg-libxdmcp-1.1.5-h9b100fa_0 xorg-libxext-1.3.6-h9b100fa_0 xorg-libxfixes-6.0.1-h9b100fa_0 xorg-libxrender-0.9.12-h9b100fa_0 xorg-libxscrnsaver-1.2.4-h9b100fa_0 xorg-xorgproto-2024.1-h5eee18b_1 yaml-0.2.5-h7b6447c_0 zstd-1.5.7-hb8e6e7a_2 Proceed ([y]/n)? y Preparing transaction: done Verifying transaction: done Executing transaction: | done (DepMamba) dw@node01:~/DepMamba-main$ 卸载完了吧
最新发布
07-31
<think>我们参考了用户提供的引用内容,特别是引用[1]和引用[2]中关于卸载PyTorch的步骤。用户询问在Linux环境下使用conda卸载PyTorch及相关库(torchvision, torchaudio)后,如何确认是否已成功卸载。 ### 确认卸载的步骤 1. **检查conda环境中的包列表** 在卸载后,首先检查当前conda环境中是否还存在PyTorch相关的包。执行以下命令: ```bash conda list | grep -E "torch|pytorch" ``` - 如果没有任何输出,说明PyTorch、torchvision、torchaudio等包已经被卸载。 - 如果还有输出,则说明仍有残留的包,可以手动卸载这些包,例如: ```bash conda uninstall <package_name> ``` 2. **尝试导入PyTorch验证** 在Python环境中尝试导入PyTorch,如果导入失败(报ModuleNotFoundError),则说明已卸载: ```bash python -c "import torch; print(torch.__version__)" ``` - 预期结果:出现`ModuleNotFoundError: No module named 'torch'`。 3. **检查pip安装的包(如果之前也用pip安装过)** 有时用户可能混合使用conda和pip安装,因此也需要检查pip的包列表: ```bash pip list | grep -E "torch|pytorch" ``` - 如果有残留,使用pip卸载: ```bash pip uninstall torch torchvision torchaudio ``` 4. **检查环境路径中是否还有残留文件** 如果怀疑有文件残留,可以检查conda环境的site-packages目录: ```bash ls $CONDA_PREFIX/lib/python*/site-packages | grep -E "torch|pytorch" ``` - 如果存在残留文件,可以手动删除(谨慎操作)。 ### 完整示例流程 假设环境名为`pytorch_env`,以下为完整步骤: ```bash # 激活环境 conda activate pytorch_env # 卸载PyTorch及相关库 conda uninstall pytorch torchvision torchaudio # 检查conda列表 conda list | grep -E "torch|pytorch" # 尝试导入(应失败) python -c "import torch" # 检查pip列表 pip list | grep -E "torch|pytorch" # 如果有pip安装的残留,则用pip卸载 pip uninstall torch torchvision torchaudio ``` ### 注意事项 - 如果卸载后`conda list`仍然显示有torch相关包,可能是因为这些包是作为依赖被其他包安装的,需要检查依赖关系。 - 在卸载过程中,conda会提示将要卸载的包列表,请确认是否包含`pytorch`、`torchvision`、`torchaudio`。 ### 相关问题 1. 如何彻底清理conda环境中残留的包文件? 2. 卸载PyTorch后如何重新安装特定版本? 3. 如何解决卸载后导入PyTorch仍然成功的问题(可能由于多环境混淆)? [^1]: Linux下卸载PyTorch的方法 [^2]: 验证PyTorch卸载的方法</think>### 确认PyTorch卸载状态的完整指南 在Linux环境下使用conda卸载PyTorch及相关库后,可通过以下方法验证是否成功卸载: #### 1. 检查conda包列表 ```bash conda list | grep -E "torch|pytorch|cudatoolkit" ``` - **预期结果**:无任何输出或仅显示`cudatoolkit`(如果未卸载CUDA) - **异常情况**:若显示`pytorch`, `torchvision`, `torchaudio`等条目,说明未完全卸载[^1][^2] #### 2. 验证Python导入 ```bash python -c "import torch; print(torch.__version__)" 2>&1 | grep "ModuleNotFoundError" ``` - **成功标志**:显示`ModuleNotFoundError: No module named 'torch'` - **失败标志**:输出版本号(如`1.12.1+cu116`)[^2] #### 3. 检查CUDA可用性 ```bash python -c "import torch; print(torch.cuda.is_available())" ``` - **成功卸载**:应显示`ModuleNotFoundError` - **若仍可导入**:显示`False`表示PyTorch存在但无法使用GPU[^2] #### 4. 查看环境路径残留 ```bash ls $CONDA_PREFIX/lib/python*/site-packages | grep -E "torch|pytorch" ``` - **干净环境**:无匹配文件 - **残留文件**:如`torch`, `torchvision`目录或`torch-*.egg-info`文件[^4] #### 5. 验证conda元数据 ```bash conda list --revisions ``` 查看最近操作记录,确认包含: ``` 2024-06-15 10:00:00 (rev 10) removed pytorch-1.12.1 removed torchvision-0.13.1 removed torchaudio-0.12.1 ``` > **注意**:若使用`pip`混合安装过PyTorch,需额外执行: > ```bash > pip list | grep torch > pip uninstall torch torchvision torchaudio # 清理pip安装的残留 > ``` ### 卸载后常见问题处理 1. **仍有残留文件**: ```bash conda clean --all # 清理缓存和未使用的包 rm -rf ~/.cache/pip # 清除pip缓存 ``` 2. **环境混乱需重建**: ```bash conda create --name clean_env --clone original_env conda activate clean_env conda uninstall pytorch torchvision torchaudio ``` ### 相关问题 1. 如何彻底清除conda环境中的所有PyTorch残留文件? 2. 卸载PyTorch后如何安装特定历史版本? 3. conda和pip混合安装的PyTorch如何完全卸载? 4. PyTorch卸载后为什么`import torch`仍可能成功? [^1]: Linux下PyTorch卸载方法 [^2]: PyTorch卸载后验证GPU可用性 [^4]: PyTorch版本与环境管理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值